作者ireullin (raison detre)
看板PHP
標題[請益] codeigniter跟資料庫取資料時會報錯
時間Mon Jul 1 17:11:11 2013
程式會報錯
Severity: Notice
Message: Trying to get property of non-object
Filename: controllers/specialty.php
Line Number: 61
$cnt確定有筆數而且是正確的
但是取資料的時候用for 或是foreach都是以上的錯誤
檢查null也沒報錯
property確定是對的
下面是我的程式碼
// controller 的程式碼
$this->load->model('SpecialtyModel');
$query = $this->SpecialtyModel->selectAll();
$row = $query->result();
$cnt = $query->num_rows();
if($row==null) die('it is null');
for($i=0; $i<$cnt; $i++)
{
$this->table->add_row(
$row[$i]->specialty_id,
$row[$i]->specialty_name
);
}
/*
foreach($row as $tmp)
{
$this->table->add_row(
$tmp->specialty_id,
$tmp->specialty_name
);
}
*/
// model的程式碼
class SpecialtyModel extends CI_Model
{
private $table="specialty";
public function __constract()
{
parent::__construct();
}
public function selectAll()
{
$this->load->database();
return $this->db->get($this->table);
}
}
版本
PHP 5.3.10-1ubuntu3.6
mysql Ver 14.14 Distrib 5.5.31,
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 219.85.131.202
1F:→ saviro:is_array($row) ? result() 應該會回傳空陣列 07/02 00:17
2F:推 saviro:用empty($row)做檢查? 07/02 00:22
3F:→ liaosankai:var_dump($row)內容出來檢查看看? 07/02 01:00