【问题标题】:php loop recordset untilphp循环记录集直到
【发布时间】:2012-03-11 06:53:12
【问题描述】:

在 php 中,如何循环一个 mysql 记录集,直到找到我需要的条件?

只是:

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

【问题讨论】:

    标签: php mysql loops while-loop


    【解决方案1】:
    if (this == that) { break; }
    

    只需跳出循环,它就会停止运行并继续执行其余代码。

    【讨论】:

      【解决方案2】:

      显而易见的答案如下:

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

      您可以对查询做些什么吗?让它为你得到正确的结果?你对this == that 条件实际上做了什么?

      【讨论】:

        【解决方案3】:

        差不多就是这样 - 查看结束搜索的其他答案。还可以考虑调整您的查询以选择您要查找的特定记录。让数据库处理搜索会更快/更有效。

        【讨论】:

          【解决方案4】:

          应该可以,但你也可以这样做:

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

          【讨论】:

            猜你喜欢
            • 2018-01-02
            • 2015-09-26
            • 1970-01-01
            • 2015-07-02
            • 2011-02-04
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多