2012-04-08 42 views
0

我有兩個功能getCompanyDetailsgetHostingDetails兩種不同的功能一個工作一個調用非對象

getCompanyDetails正常工作的第一個數據庫,但getHostingDetails顯示

Trying to get property of non-object

getCompanyDetails:

控制器:$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(); 
    } 
} 

getHostingDetails:

控制器:

$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(); 
    }    
} 

回答

1

那麼,一個方法返回從$result->row()和其他false的對象。你不能在false上調用方法。

false在未找到記錄時返回。所以你需要在使用它之前檢查返回值。

+0

:)我怎樣才能顯示錯誤信息?我有一個公平的想法,如果另一個聲明 – 2012-04-08 02:21:46

1

那麼在你get功能的機會是你的代碼可能會返回你false如果沒有行返回。您可能希望在檢索詳細信息之前進行檢查。例如:

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

問題可能是你如何在你的控制器中使用這些功能。如果其中任何一個返回FALSE,那麼

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

會給你錯誤。嘗試

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