开发者

Why does a one-line mysql query/fetch array statement not work?

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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜