我有一個類系統和一個函數,它從數據庫結果做foreach。變量是在foreach中分配的,但在foreach外部是空的。PHP變量空外部Foreach?
// Top of file
private $movieList = array();
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
// add each to the array
$this->movieList[] = array('nid' => $row->nid, 'title' => $row->title, 'movie_pos_id' => $row->movie_pos_id);
print_r($this->movieList); // variable full of stuff
}
// No results found
return false;
}
print_r($this->movieList); // variable empty
任何想法爲什麼?
你可以發佈整個代碼;我的打賭是你在循環實際運行之前打印它 – scibuff 2012-03-06 16:25:32
這不是'$ query-> num_rows()> 0'。 – hakre 2012-03-06 16:25:53