如何遍历 PDOStatement 对象,即获取第一个、最后一个、上一个、下一个等?

发布于 2024-12-10 04:58:02 字数 853 浏览 0 评论 0原文

我试图在网页中一次又一次地访问数据。有更好的办法吗?像 movetofirst()、movetolast()、movetoprevious()、movetonext() 这样的东西可能会很好。

现在我正在将结果集作为数组检索(使用 fetchall())并一次又一次地重新使用该数组。

有像下面这样的事情要做吗?我不需要一次又一次地执行查询。如果结果/数组有数百行,则将数据保存在数组中并消耗资源。

$sql = 'SELECT cityname, statename FROM TBLPLACES ORDER BY cityname, statename';
$stmt = $conn->query($sql);
if ($stmt) {
    while($row=$stmt->fetch()){
       // do some thing
    }

    // do some more thing
    //
    // now here, can i access same $stmt object
    // to fetch resultset again without executing
    // $stmt = $conn->query($sql); again ?
    // (no change in query sql, need to fetch the same static data again.)
    //
    // something like below will be nice.
    //
    // $stmt->movetofirst();
    // while($row=$stmt->fetch()){
    //   do some thing;
    // } 
}

I am trying to access data again and again in a webpage. Is there a better way ? some thing like movetofirst(), movetolast(), movetoprevious(), movetonext() could be nice.

right now i am retrieving the resultset as an array (using fetchall()) and resusing the array again and again.

Is there something like below be done? I need not execute the query again and again. keep the data in a array and consume resources if the result/array is of many hundred rows.

$sql = 'SELECT cityname, statename FROM TBLPLACES ORDER BY cityname, statename';
$stmt = $conn->query($sql);
if ($stmt) {
    while($row=$stmt->fetch()){
       // do some thing
    }

    // do some more thing
    //
    // now here, can i access same $stmt object
    // to fetch resultset again without executing
    // $stmt = $conn->query($sql); again ?
    // (no change in query sql, need to fetch the same static data again.)
    //
    // something like below will be nice.
    //
    // $stmt->movetofirst();
    // while($row=$stmt->fetch()){
    //   do some thing;
    // } 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝戈者 2024-12-17 04:58:02

要获取最后的结果,您可以执行以下操作:

$row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_LAST);

或手动写入行号。其他 PDO::FETCH_ORI_* 常量此处,但是本质上你想更多地关注 PDOStatement::fetch() 方法。

To fetch last result you can do:

$row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_LAST);

Or write the row numbers manualy. Other PDO::FETCH_ORI_* constants here, but essentially you want to pay more attention to the 2nd and 3rd parameter in PDOStatement::fetch() method.

落墨 2024-12-17 04:58:02

只需使用 fetchAll 即可获取整个结果大批:

$sql = 'SELECT cityname, statename FROM TBLPLACES ORDER BY cityname, statename';
$stmt = $conn->query($sql);
// You should not silently ignore errors.
// Set PDO error mode to PDO::ERRMODE_EXCEPTION to handle query failure
$data = $stmt->fetchAll();

foreach ($data as $row) {
  // Do one thing
}
foreach ($data as $row) {
  // Do another thing
}

Just use fetchAll to get the whole result in an array:

$sql = 'SELECT cityname, statename FROM TBLPLACES ORDER BY cityname, statename';
$stmt = $conn->query($sql);
// You should not silently ignore errors.
// Set PDO error mode to PDO::ERRMODE_EXCEPTION to handle query failure
$data = $stmt->fetchAll();

foreach ($data as $row) {
  // Do one thing
}
foreach ($data as $row) {
  // Do another thing
}
甲如呢乙后呢 2024-12-17 04:58:02

使用 PDO,您不需要创建查询来获取上一个或下一个,
尝试 juste : PDO::CURSOR_SCROLL

参考(示例 2):
http://php.net/manual/en/pdostatement.fetch.php

with PDO you don't need to create a query to get previous or next,
try juste : PDO::CURSOR_SCROLL

reference (example 2):
http://php.net/manual/en/pdostatement.fetch.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文