<?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++;
}
}
//$j=0;
//while($j<$i)
$j=0;
while($j<$i)
{
$result16=mysql_query("select * from dish_box where dish_name='$dish[$j]'");
while($row=mysql_fetch_array($result16))
{
$v_id=$row['dish_id'];
echo $v_id.'<br />';
}
$j++;
}
mysql_close();
?>
此while循环仅回显一次值。请弄清楚为什么循环工作一次?
答案 0 :(得分:1)
或许更好的解决方案:
// if you're using user input here, be sure to use mysql_real_escape_string
/*
$dishes = "'";
foreach( $dish as $key => $val )
$dishes .= "'" . mysql_real_escape_string( $val ) . "',";
$dishes = substr( $dishes, 0, strlen( $dishes ) -2 ); // remove last ,
*/
// otherwise implode the list
$dishes = "'".implode("','", $dish)."'";
// in either case use the IN operator
// the semi-colon that was in the query won't work.
$result16=mysql_query("select * from dish_box where dish_name in ($dishes)");
// to confirm how many rows
echo "result16 returned " . mysql_num_rows($result16) . 'rows.';
while($row=mysql_fetch_array($result16))
{
$v_id=$row['dish_id'];
echo $v_id.'<br />';
}
我刚刚回忆起你在another question做了类似的事情。实际上,您可以将两个查询组合在一起并节省一些时间:
$result16=mysql_query("select * from dish_box where dish_name in ".
"(select c.dishes from c)");
while($row=mysql_fetch_array($result16))
{
$v_id=$row['dish_id'];
echo $v_id.'<br />';
}
答案 1 :(得分:1)
您在$result16
中存储的查询很可能只返回一行。尝试做:
$result16 = mysql_query(...);
echo "Total rows: ", mysql_num_rows($result16), "<br />";
while (...) {
etc.....
同样,您可能想尝试:
$result16 = mysql_query(...) or die(mysql_error());
假设您的查询成功,只会在事情失败时引起痛苦。
答案 2 :(得分:0)
$i=5;
$j=0;
while($j<$i)
{
$result16=mysql_query("select * from dish_box where dish_name='$dish[$j]'");
while($row=mysql_fetch_array($result16))
{
$v_id[]=$row['dish_id'];
}
$j++;
}
var_dump($v_id);
你用每个循环覆盖$ v_id的值 - 因此它只会返回最后一个值。使$ v_id成为一个数组并写入它将有助于