<?php
include 'db.php';
$i=0;
$result15=mysql_query("select c.dishes from c");
while($row=mysql_fetch_array($result15))
{
if($row['dishes']!=NULL)
{
$dish[$i]=$row['dishes'];
$i++;
}
}
mysql_close();
$j=0;
while($j<=$i)
{
echo $dish[$j];
$j++;
}
?>
得到通知:第 18 行 F:\xampp\htdocs 中的未定义偏移量:2
最佳答案
你的意思是while($j<$i)
那里。
请记住,您在最后一次插入后 递增了 $i。这意味着 $i 将高于 $dish 的最大键。
一些想法:
任何时候你用 null 测试相等性,你应该考虑使用 is_null
(或!is_null)。它更准确。
这个:
$dish[$i]=$row['dishes'];
$i++;
会更好:
// obviously instead of $i you would use count($dish) later (or use foreach)
$dish[]=$row['dishes'];
最后的 while 循环作为 foreach 会更好:
foreach($dish as $val)
{
echo $val;
}
关于PHP/MySQL 数组帮助需要,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6782211/