【发布时间】:2013-06-06 13:43:33
【问题描述】:
我正在尝试使用 foreach 循环设置值以填充表,由于某种原因,我的数组在值内显示为空。但是当我 var dump $result_list[] 它显示产品数组时,我做错了吗?谢谢
$result = mysql_query("SELECT id, product, price FROM products");
$result_list = array();
while($row = mysql_fetch_array($result)) {
$result_list[] = $row;
}
foreach($result_list as $row) {
$productitems[] = array(
'id' => $row->id,
'product' => $row->product,
'price' => $row->price
);
}
var_dump($productitems);
array(2) { [0]=> array(3) { ["id"]=> NULL ["product"]=> NULL ["price"]=> NULL } [1]=> array(3) { ["id"]=> NULL ["product"]=> NULL ["price"]=> NULL } }
【问题讨论】:
-
不是 $row->id 而是 $row['id']
-
不要使用已弃用的
mysql_*函数。 -
The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead。反正。你需要 foreach($result_list[] as $row。你忘记了 []。