两个不同的函数,一个工作,一个调用非对象


Two different functions one works one calls a non-object

我有两个函数getCompanyDetailsgetHostingDetails

第一个数据库getCompanyDetails工作正常,但getHostingDetails显示

Trying to get property of non-object

获取公司详细信息:

控制器:$data['companyName'] = $this->quote->getCompanyDetails()->companyName;

型:

public function getCompanyDetails()
{
    $this->db->select('companyName,companySlogan,companyContact,
                       companyEmail,companyWebsite,companyPhone,
                       companyFax,companyAddress');
    $this->db->from('companyDetails');
    $result = $this->db->get();
    if($result->num_rows()<1)
    {
        return FALSE;
    }else{
        return $result->row();
    }
}

获取托管详细信息:

控制器:

$data['hostingRequired'] = $this->quote->getHostingDetails()->hostingRequired;

型:

public function getHostingDetails()
{
    $this->db->select('hostingRequired,domainRequired,domainToBeReged,
                       domaintoBeReged0,domainTransfer,domainToBeTransfered,
                       domainToBeTransfered0,currentHosting');
    $this->db->from('hostingDetails');
    $result = $this->db->get();
    if($result->num_rows()<1)
    {
        return FALSE;
    }else{
        return $result->row();
    }               
}

好吧,一种方法从$result->row()返回一个对象,而另一个方法则false。不能在 false 上调用方法。

未找到记录时返回false。因此,您需要在使用前检查返回值。

在你的get函数中,如果没有返回行,你的代码可能会返回你false。您可能需要在检索详细信息之前进行检查。 例:

$details = $this->quote->getHostingDetails();
if($details){
    $data['hostingRequired'] = $details->hostingRequired;
}

问题可能在于如何在控制器中使用这些函数。如果其中任何一个返回 FALSE,则

$this->quote->getHostingDetails()->hostingRequired;

会给你错误。尝试

if ($row = $this->quote->getHostingDetails()) {
    echo $row->hostingRequired;
}