使用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.
我正在尝试通过php运行查询到我的mysql数据库,并且我试图获取所有结果行。 我还必须将每一行与返回的下一行进行比较。 我试图通过将结果变量设置为另一个临时变量并在其上调用 以下是代码示例: p>
那么当我调用 我们将不胜感激。 p>
div> mysqli_fetch_assoc() code>来做到这一点,以便while循环再次运行下一行。 但是当我尝试在其他变量上使用
mysqli_fetch_assoc() code>时,不知何故
mysqli_fetch_assoc($ result) code>也会进展到下一行的下一行 while($ row = mysqli_fetch_assoc($ result)) code>进入下一次迭代。 p>
$ query =“SELECT * FROM records ORDER BY num ASC;”;
if($ result = mysqli_query($ conn,$ query))
{
while($ row = mysqli_fetch_assoc($ 结果))
{
$ temporaryresult = $ result;
$ rowtwo = mysqli_fetch_assoc($ temporaryresult); //这使得mysqli_fetch_assoc($ result)跳过下一行不需要的行
}
}
pre>
mysqli_fetch_assoc($ temporaryresult) code>时,如何让
mysqli_fetch_assoc($ result) code>继续前进? p>
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.
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;
}