[請益] codeigniter跟資料庫取資料時會報錯

看板PHP作者 (raison detre)時間12年前 (2013/07/01 17:11), 編輯推噓1(102)
留言3則, 2人參與, 最新討論串1/2 (看更多)
程式會報錯 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
is_array($row) ? result() 應該會回傳空陣列
07/02 00:17, 1F

07/02 00:22, , 2F
用empty($row)做檢查?
07/02 00:22, 2F

07/02 01:00, , 3F
var_dump($row)內容出來檢查看看?
07/02 01:00, 3F
文章代碼(AID): #1HqKUohS (PHP)
文章代碼(AID): #1HqKUohS (PHP)