0
<?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?

2
  • 1
    1) var_dump($dish) outside the loop to inspect it, and; 2) make sure each version of the query matches at least one record in your database table, otherwise the inner while will be skipped. Commented Jul 21, 2011 at 21:10
  • 1
    Please also add sample data in your tables of c and dish_box Commented Jul 21, 2011 at 21:39

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Checking whether a query has failed is good, but there can't be enough mentions that the or die(mysql_error()); is for during development, and an actual failure on a live site can be handled differently for a far better user experience.
1

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

5 Comments

Euhm, escaping might be nice.
@Wrikken look at the other question I cited. I am fairly certain these are flip-sides of the same coin.
OK, the second answer is probably more correct. Do do something about non-escaped values in the first though...
I've added the escape code as comment, but, in this case, I don't think its needed.
My prbolem not solved yet... i have updated the complete source code. Please give it a look
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);

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

1 Comment

No, the value gets echoed right after assignment, so thats not a problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.