当前位置: 动力学知识库 > 问答 > 编程问答 >

mysql - php loop recordset until

问题描述:

In php, how can I loop a mysql recordset until ive found the criteria I need?

is it just:

$sql_result = mysql_query("SELECT * FROM table", $db);

while ($rs = mysql_fetch_array($sql_result) && $done == 0) {

if (this == that) { $done = 1; }

}

网友答案:
if (this == that) { break; }

Simply break out of the loop, it'll stop running and carry on with the rest of the code.

网友答案:

The immediately obvious answer is the following:

$sql_result = mysql_query("SELECT * FROM table", $db); 
while ($rs = mysql_fetch_array($sql_result) && $done == 0) {
    if (this == that) {
        break;
    }
}

carry_on();

Is there something you can do with your query though? To make it just get the correct result for you? What is it you're actually doing with your this == that condition?

网友答案:

That's pretty much it - see the other answers for ending that search. Also consider adjusting your query to select the specific record you're looking for. Letting the database handle the search will be much faster/more efficient.

网友答案:

That should work, but you can also do:

$sql_result = mysql_query("SELECT * FROM table", $db); 
while ($rs = mysql_fetch_array($sql_result)) {
  if (this == that) { 
    break;
  }
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: