PHP / MYSQL while循环问题

<?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();
 ?>

This while loop is echoing value only once. Please figure it out why loop is working onlu once?

Better solution perhaps:

// 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 />';
}

I just recalled that you were doing something like this in another question. You actually can combine both queries and save yourself some time:

$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 />';
}

Most likely the query you store in $result16 is only returning a single row. Try doing:

$result16 = mysql_query(...);
echo "Total rows: ", mysql_num_rows($result16), "<br />";
while (...) {
etc.....

As well, you might want to try doing:

$result16 = mysql_query(...) or die(mysql_error());

Assuming your query has succeeded will only cause pain down the road when things fail.

     $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);

You are over-writing the value of $v_id with each loop- hence it will only return the last value. making $v_id an array and writing to it will help