如何在使用PHP/MYSQL循环时检查下一行的值?

如何在使用PHP/MYSQL循环时检查下一行的值?,php,Php,我用这个代码得到了上一行记录 <?php $previousRow = array(); while ($temp = mysql_fetch_row($res2)) { echo "<br>currentRow:".$temp[1]; echo "previousRow:".$previousRow[1]; $previousRow = $temp; } ?> 输出 当前行:1前一行: 当前行:5先前行:1

我用这个代码得到了上一行记录

<?php
  $previousRow = array();
  while ($temp = mysql_fetch_row($res2)) 
 {

     echo "<br>currentRow:".$temp[1];
     echo "previousRow:".$previousRow[1];
     $previousRow = $temp; 

  } 
 ?>

输出 当前行:1前一行:

当前行:5先前行:1

当前行:6上一行:5

当前行:7上一行:6

当前行:8上一行:7

如何检查由上一行替换的下一行的值


任何帮助都将不胜感激。

我会先收集所有行,然后用一个for:

<?php
$rows = array();
while ($temp = mysql_fetch_row($res2)) 
{
    $rows[] = $temp;
}
$rowCount = count($rows);
for ($i = 0; $i < $rowCount; $i++) {
     echo "<br>currentRow:".$rows[$i][1];
     if ($i > 0) {
         echo "previousRow:".$rows[$i - 1][1];
     }
         if ($i + 1 < $rowCount - 1) {
             echo "nextRow:".$rows[$i + 1][1];
         }
} 
?>

如果我没弄错,那么像这样的东西会有帮助吗

$previousRow = array();
$currentRow = mysql_fetch_row($res2);

while ($currentRow) {
    $nextRow = mysql_fetch_row($res2);

    echo "<br>currentRow:".$currentRow[1];
    echo "previousRow:".$previousRow[1];
    echo "nextRow:".$nextRow[1];

    $previousRow = $currentRow;
    $currentRow = $nextRow;
}
$previousRow=array();
$currentRow=mysql\u fetch\u行($res2);
while($currentRow){
$nextRow=mysql\u fetch\u行($res2);
回声“
currentRow:”.$currentRow[1]; 回显“previousRow:”.$previousRow[1]; echo“nextRow:”.$nextRow[1]; $previousRow=$currentRow; $currentRow=$nextRow; }
请尝试下面给出的代码

$res = array();
while ($result = mysql_fetch_row($r)) {
    $res[] = $result;
 }
 echo "<pre>";
 foreach($res AS $index=>$res1){
     echo "Current".$res1[1]; 
     echo "  Next" . $res[$index+1][1];
     echo "  Prev" . $res[$index-1][1]; echo "<br>";
 }
$res=array();
而($result=mysql\u fetch\u row($r)){
$res[]=$result;
}
回声“;
foreach($resas$index=>$res1){
回显“当前”。$res1[1];
回显“下一步”。$res[$index+1][1];
echo“Prev”。$res[$index-1][1];echo“
”; }
谢谢