0

Shows "Undefined variable: display1". I am able to display the while loop. The problem is $display1 had define in while loop and it must in it because have $i increment.

//SQL for display category name
$query1="SELECT cat_name FROM restaurant_category WHERE rest_id={$display}";
$result1=mysql_query($query1);
$rowNum1=mysql_num_rows($result1);
//SQL for display dish information of each category
$query3="SELECT cat_id FROM restaurant_category WHERE cat_name={$display1}";
$result3=mysql_query($query3);
$display3=mysql_query($result3);
$query2="SELECT dish_name, dish_description, dish_price FROM dish WHERE cat_id={$display3}";
$result2=mysql_query($query2);
$rowNum2=mysql_num_rows($result2);
$data=mysql_fetch_row($result2);

while($i<$rowNum1)
{
$display1=mysql_result($result1,$i,"cat_name");
//display category name
    while($j<$rowNum2)
    {//display dish information}
}
2
  • php.net/manual/en/language.variables.scope.php Commented Jul 4, 2013 at 19:31
  • 3
    PHP is not going to time travel for you and execute the while loop to fetch results BEFORE you've actually run the query to PRODUCE the results in the first place. Commented Jul 4, 2013 at 19:32

2 Answers 2

1
//SQL for display dish information of each category
$query3="SELECT cat_id FROM restaurant_category WHERE cat_name={$display1}";
$result3=mysql_query($query3);
$display3=mysql_query($result3);

This appears to be an error, calling mysql_query on $result3. It should probably be

$display3 = mysql_fetch_row($result3)

And here:

$display1 = null;
while($i<$rowNum1)
{
$display1=mysql_result($result1,$i,"cat_name");
//display category name
    while($j<$rowNum2)
    {
       //display dish information
    }
}

Possible syntax errors in there. Also add $display1 outside of the while scope.

Also oblig: mysql_* functions are depreciated.

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

3 Comments

Declaring $display1 outside makes no difference in PHP. They are not using it anywhere -- at least in the code shown.
OP wants to time travel and use $display1 before it's ever been assigned a value.
If you actually read the code, they assign $display1 a value. That's what a "=" sign means in PHP. Assignment. See comments such as "display category name" probably has something to do with $display1 variable after its assignment.
0

The $display1 variable will be defined after the while loop. The warning you are seeing is because you are assigning to a variable you haven't used before inside a loop. Simply declare the variable outside the while loop to get rid of the warning:

$display1 = null;
while ($i<...

Comments

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.