简体   繁体   English

运行两个连续的while循环时的光标位置问题

[英]Cursor position problems when running two consecutive while loops

How does one "reset" pg_fetch_row cursor position to start with the first row in case you want to run through the same result set twice? 如果要两次遍历同一结果集,如何“重置” pg_fetch_row光标位置以第一行开头?

$result = pg_query($conn, "SELECT author, email FROM authors");
if (!$result) {
  echo "An error occurred.\n";
  exit;
}

while ($row = pg_fetch_row($result)) {
  echo "Author: $row[0]  E-mail: $row[1]";
  echo "<br />\n";
}

while ($row = pg_fetch_row($result)) {     // <---- THIS WILL ALWAYS SHOW NOTHING AS IT DOESN'T START FROM the first row (0)
  echo "Author: $row[0]  E-mail: $row[1]";
  echo "<br />\n";
}

Use pg_result_seek() http://php.net/manual/en/function.pg-result-seek.php 使用pg_result_seek() http://php.net/manual/zh/function.pg-result-seek.php

$result = pg_query($conn, "SELECT author, email FROM authors");
if (!$result) {
  echo "An error occurred.\n";
  exit;
}

while ($row = pg_fetch_row($result)) {
  echo "Author: $row[0]  E-mail: $row[1]";
  echo "<br />\n";
}

pg_result_seek($result, 0);

while ($row = pg_fetch_row($result)) {
  echo "Author: $row[0]  E-mail: $row[1]";
  echo "<br />\n";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM