简体   繁体   中英

what's wrong with this piece of php code?

I am trying to put an indeterminate amount of names into variable values using the loop statement:

while ($row = mysql_fetch_row($resultdq2))
    {
        $name[$i] = $row[2];
        $parent_ID = $row[1];
        $i++;
    }   

Then i am trying to echo the names using the following structure/

$x = 0;
while (isset ($name[$x]))
echo $name[$x];
$x++;

The only problem is, this second loop structure freezes my pc onload, thus i am assuming that it is looping infinitely.

Why is it doing such? assuming that $x and $i are the same value (ie 7) then $name[$x] and name[$i] should be equivalent, is that not correct?

Then once my $name[$x] condition in my second loop passes the point of variables assigned, shouldnt the loop terminate?

Is it possible that for the loop statement condition, it is using the global variable declaration $x = 0 instead of the internal $x value to evaluate if the condition is met, thus the statement is always true?

What is the problem, and how can i correct it?

Maybe add '{' and '}' to increase the counter?

while (isset ($name[$x])) {
  echo $name[$x];
  $x++;
}

Better use foreach , add is_array check. Write nice code!

Only echo $name[$x]; is inside while loop. It should be:

$x = 0;
while (isset ($name[$x]))
{
    echo $name[$x];
    $x++;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM