Why does the mysql single line query / fetch statement not work?

I have encountered this problem several times and I cannot understand why this is happening. When I create a MySQL statement as shown below, it works 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 operators with the following:

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

and try to skip them, the page seems to work endlessly without loading or returning an error. Why doesn't the second expression work?

+4
source share
1 answer

mysql_query executes the query and returns the record identifier, which mysql_fetch_array used to retrieve the next row. When you join them together, as you tried to do, each iteration of the while will trigger a query, return a new record identifier, and retrieve the first row. As long as there is at least one line, it will end up doing it in an infinite loop.

+6
source

Source: https://habr.com/ru/post/1333964/


All Articles