如何在mysqli结果中获得下一行


How to get the next row in mysqli result?

我想在mysql结果的循环中获得下一行,以编写具有下一个和上一个元素代码的列表,如以下示例:

<?php $previous = null;
$next = null;
while ($row = $result->fetch_assoc()) {
$next = ??????????????? // how to get it?
?>
<li code="<?php echo $row['code']; ?>" previous="<?php echo $previous; ?>" next="<?php echo $next; ?>">Hello world!</li>
<?php
$previous = $row['code']; // previous is easy to get...
} ?>

不确定MySQLi,但这里是我如何在PDO:

$data = $stmt->fetchAll('PDO::FETCH_ASSOC);
for($i = 0; $i < count($data); $i++) {
   $next = isset($data[$i+1]) ? $data[$i+1] : null;
}
如果mysqli中没有fetchAll,则执行:
while ($row = $result->fetch_assoc()) {
   $data[] = $row;
}
if ( ! empty($data)) {
  for($i = 0; $i < count($data); $i++) {
     $next = isset($data[$i+1]) ? $data[$i+1] : null; // next
     $prev = isset($data[$i-1]) ? $data[$i-1] : null; // previous
     $curr = isset($data[$i])   ? $data[$i]   : null; //current
  }
}

不如这样写:

SELECT * FROM table WHERE id > '$currentID' ORDER BY id LIMIT 1;

看看这个解决方案,你觉得怎么样?它是有效的,但是会更慢吗?这个算法至少需要两次通过吗?你觉得呢?

$db = DB::getConnection();
$result = $db->query( "SELECT * FROM objects" );
$next = null;
$previous = null;
$temp_previous = null;
$print = false;
$row = null;
$fetchit = false;
$code = null;
echo "<ul>";
while (true)
{
if ($print) {
   if ($fetchit) {
    $row = $result->fetch_assoc();
        $next = $row['code'];
   }
   echo "<li code='$code' next='$next' previous='$previous'>{$row['description']}</li>" . endl();
   $previous = $temp_previous;
   $print = false;
}
else {
 if (!$fetchit)
     $row = $result->fetch_assoc();
 $code = $row['code'];
 $temp_previous = $row['code'];
 $print = true;
 $fetchit = true;
}
if(!is_array($row)) break;
}
echo "</ul>";