4

I have run into this problem multiple times, and I cannot figure out why it happens. When I build a MySQL statement like the following it works just fine:

$sql=mysql_query("SELECT * FROM table WHERE id='$something'");
while($row=mysql_fetch_array($sql)){
    $someVar=$row['whatever'];
    //and so on
    }

But when I combine the first two statements to the following:

while($row=mysql_fetch_array(mysql_query("SELECT * FROM table WHERE id='$something'")))

and try to loop through them, the page seems to loop infinitely without loading or returning an error. Why doesn't the second statement work?

1 Answer 1

6

mysql_query executes the query and returns a record id which mysql_fetch_array uses to fetch the next row. When you chain them together like you've tried doing, each iteration of the while loop will exectute the query, return a new record id and fetch the first row. As long as there's at least one row, it'll end up doing this in an infinite loop.

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

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.