dongzice4895
2017-03-17 11:58 阅读 81
已采纳

使用mysqli_fetch_assoc()时如何在php中获取下一行MySQL查询而不进行操作

I am trying to run a query to my mysql database through php and and am trying to get all the resulting rows. I also have to compare every row to the next row returned. I am trying to do this by setting the result variable to another temporary variable and calling mysqli_fetch_assoc() on that so that the while loop runs again for the next row. But what happens is that when I try to use mysqli_fetch_assoc() even on the other variables, somehow mysqli_fetch_assoc($result) also progresses to the next of the next row when while($row = mysqli_fetch_assoc($result)) goes to next iteration.

Here is the code example to illustrate this :

$query = "SELECT * FROM records ORDER BY num ASC;";
    if($result = mysqli_query($conn, $query))
    {
       while($row = mysqli_fetch_assoc($result))
       {
         $temporaryresult = $result;
         $rowtwo = mysqli_fetch_assoc($temporaryresult);// this makes mysqli_fetch_assoc($result) skip the next row which is unwanted
       }
    }

So how can I keep mysqli_fetch_assoc($result) from moving forward when I call mysqli_fetch_assoc($temporaryresult) ?

Any help would be appreciated.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    dpsr1670 dpsr1670 2017-03-17 12:48

    After @CBroe's answer, I tried to solve this problem while still trying to look forward. I achieved this by storing the rows returned by the database and then looping through them. This makes it very easy too look ahead in the rows returned while avoiding the complexity of changing your code to look backwards.

    $array = array(); 
    // look through query
    while($row = mysql_fetch_assoc($query)){    
      // add each row returned into an array
    
      $array[] = $row;
    
    }
    

    Now, looping through these rows,

    $i = 0;
    for(;$i<count($array)-1;$i++)
    {
       if($array[$i]['somecolumn']==$array[$i+1]['anothercolumn'])//compare this column to another column in the next row
       {
          // do something
       }
    } 
    

    This successfully solved my problem. I hope it helps anyone stuck in the same position I was in.

    点赞 评论 复制链接分享
  • dongliang9682 dongliang9682 2017-03-17 12:05

    am trying to do this by setting the result variable to another temporary variable and calling mysqli_fetch_assoc() on that so that the while loop runs again for the next row

    It doesn’t work that way. Just because you assigned the resource id to a second variable, doesn’t mean that you now have a second result set that you could operate on separately. Both variables refer to the same resource id. Fetching a row will still move the row pointer of the “original” data set.

    I also have to compare every row to the next row returned

    Most likely, you are making things harder on yourself by trying to look ahead. Stuff like this is usually easier done when you look at the previous row instead. That one you have fetched already - so you don’t need to do an additional fetch now that would mess with the row pointer.

    Pseudo code example:

    $prevRow = null;
    while($row = fetch(...)) {
      if($prevRow) { // for the first row, this will still be null, so we only 
                     // start comparing stuff when that is not the case
        // compare whatever you need to compare here
      }
      ...
      $prevRow = $row;
    }
    
    点赞 评论 复制链接分享

相关推荐