問題描述
我有兩個 while 循環一個接一個地運行(不是在彼此內部) - 我已經稍微簡化了代碼,因此下面只列出了它的重要部分.當我比較兩個回顯查詢時出現問題,因為第二個 while 循環顯然根本沒有運行.
I have two while loops running one after the other (not inside of each other) - I've simplified the code a bit so that only the important parts of it are listed below. The problem arises when I compare the two echoed queries because the 2nd while loop apparently isn't running at all.
我在某處讀到有人通過對第二個循環使用 for 循環解決了這個問題,但我想深入了解為什么我的代碼中沒有運行第二個 while 循環.
I read somewhere that someone got around the problem by using a for loop for the second one but I want to get down to why exactly the second while loop is not running in my code.
$query_work_title = "SELECT title FROM works WHERE ";
while ($row = mysql_fetch_assoc($result_work_id)) {
$query_work_title .= "OR '$work_id' ";
}
echo $query_work_title;
echo '<br />';
$result_work_title = mysql_query($query_work_title) or
die(mysql_error($cn));
// retrieve the authors for each work in the following query
$query_author_id = "SELECT author_id FROM works_and_authors WHERE ";
while ($row = mysql_fetch_assoc($result_work_id)) {
$query_author_id .= "work_id = 'hello' ";
}
echo $query_author_id;
推薦答案
MySQL 擴展跟蹤每個結果的內部行指針.它在每次調用 mysql_fetch_assoc() 后遞增該指針,并且允許您使用 while 循環而不指定何時停止.如果您打算多次遍歷結果集,則需要將此內部行指針重置回 0.
The MySQL extension keeps track of an internal row pointer for each result. It increments this pointer after each call to mysql_fetch_assoc(), and is what allows you to use a while loop without specifying when to stop. If you intend on looping through a result set more than once, you need to reset this internal row pointer back to 0.
為此,您將mysql_data_seek() 在第一個循環之后:
To do this, you would mysql_data_seek() after the first loop:
while ($row = mysql_fetch_assoc($result_work_id)) {
$query_work_title .= "OR '$work_id' ";
}
mysql_data_seek($result_work_id, 0);
這篇關于第二個while循環沒有運行.為什么?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!