PHP / MySQL数组帮助需要 [英] PHP/MySQL array help need
问题描述
<?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] = $行[ '菜'];
$ i ++;
}
}
mysql_close();
$ j = 0;
while($ j <= $ i)
{
echo $ dish [$ j];
$ j ++;
}
?>
获取通知:未定义偏移量:2在F:\xampp\htdocs .... on第18行
您的意思是 while($ j <$ i)
在那里。
记住,你在最后一次插入后增加了$ i 。这意味着$ i将高于$ dish的最大关键点。
有些想法:
任何你应该考虑使用 is_null
(或!is_null)。
$ $ $ I] = $行[ '菜'];
$ i ++;
会更好:
<$ p $ ($或者使用foreach)
$ dish [] = $ row ['dishes']; //显然,而不是$我会在后面使用count($ dish)
最后while循环会比foreach更好:
foreach($ dish as $ val)
{
echo $ val;
}
<?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++;
}
?>
Getting notice: Undefined offset: 2 in F:\xampp\htdocs....on line 18
You mean while($j<$i)
there.
Remember, you incremented $i after the last insert. This means that $i will be higher than the maximum key of $dish.
Some thoughts:
Any time you're testing for equality with null, you should consider using is_null
(or !is_null). It is more accurate.
This:
$dish[$i]=$row['dishes'];
$i++;
Would be better as:
// obviously instead of $i you would use count($dish) later (or use foreach)
$dish[]=$row['dishes'];
That final while loop would be better as a foreach:
foreach($dish as $val)
{
echo $val;
}
这篇关于PHP / MySQL数组帮助需要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!