[請益] codeigniter跟資料庫取資料時會報錯
程式會報錯
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
→
07/02 00:17, , 1F
07/02 00:17, 1F
推
07/02 00:22, , 2F
07/02 00:22, 2F
→
07/02 01:00, , 3F
07/02 01:00, 3F
討論串 (同標題文章)
完整討論串 (本文為第 1 之 2 篇):
PHP 近期熱門文章
PTT數位生活區 即時熱門文章
4
18
6
16