【问题标题】:Reset php pointer?重置php指针?
【发布时间】:2014-06-18 10:04:17
【问题描述】:

我正在尝试执行以下操作:

while ($row = mysql_fetch_assoc($result){
    ...
}
...
while ($row2 = mysql_fetch_assoc($result){
    ...
} 

但它不起作用。第二个循环中的命令永远不会运行。我猜这与mysql函数的指针有关,必须重置。如果是这样,该怎么做?

【问题讨论】:

标签: php mysql pointers reset


【解决方案1】:

使用函数mysql_data_seek

http://www.php.net/manual/en/function.mysql-data-seek.php

虽然你应该考虑升级到不使用 mysql_* 函数

【讨论】:

    【解决方案2】:

    避免使用以mysql_ 开头的任何函数,它们已被弃用。使用PDO,你可以做你想做的事:

    $pdo = new PDO(/**/);                // [1]
    
    $stmt = $pdo->prepare(/* query */);  // [2]
    $stmt->execute();                    // [3]
    
    $rows = $stmt->fetchAll();           // [4]
    
    foreach($rows as $row)
    {
        //
    }
    
    foreach($rows as $row2)
    {
        //
    }
    

    上面引用的行指南:

    【讨论】:

      【解决方案3】:
      $result2 = $result;
      while ($row = mysql_fetch_assoc($result){
          ...
      }
      
      ...
      
      while ($row2 = mysql_fetch_assoc($result2){
          ...
      } 
      

      【讨论】:

      • @MarkM 首先你必须将 $result 保存到另一个变量中
      猜你喜欢
      • 2015-06-19
      • 2011-10-05
      • 2010-09-24
      • 1970-01-01
      • 1970-01-01
      • 2012-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多