如何在没有 FETCHALL 的情况下在 MYSQL PDO 中获取 2 次

How to fetch 2 times in MYSQL PDO without FETCHALL(如何在没有 FETCHALL 的情况下在 MYSQL PDO 中获取 2 次)
本文介绍了如何在没有 FETCHALL 的情况下在 MYSQL PDO 中获取 2 次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有这个示例查询:

$STH = $DBH->query("SELECT id FROM table");

我想获取第一行,然后循环显示所有行.所以我使用以下内容来获取第一行:

I want to get the first row and then loop and display all rows. So I use the following to get the first row:

$STH->setFetchMode(PDO::FETCH_ASSOC);
$first_row = $STH->fetch();
$first_row = $first_row['id'];

我使用 while 循环再次显示所有行:

I use while loop to display all rows again:

while ($list = $STH->fetch()) {      
$id = $list['id'];
echo $id;
}

现在 while 跳过第一行,我希望它被显示.是否有等效于 mysql_data_seek 将指针再次重置到第一行?我知道 fetchall 可以使用,但它对内存很不利,而且很浪费.我也可以运行查询并限制为 1,但不建议这样做,因为我有一个连接多个表的查询,并且速度非常慢.还有其他解决办法吗?

Now the while skips the first row and I want it to be displayed. Is there an equivalent to mysql_data_seek to reset the pointer again to the first row? I know fetchall can be used but it's bad on memory and wasteful. I could also run the query and limit to 1 but this is not recommended as I have a query that joins multiple tables and would be very slow. Is there any other solution?

谢谢

推荐答案

我认为你可以使用光标方向常量来选择结果......示例代码即将推出......我还没有尝试过,所以你可以需要玩一会儿.这也是基于这样一个假设:PDO::FETCH_ORI_FIRST 的作用类似于 data_seek,并将光标留在第一个位置,而不是将其返回到之前的位置.

I take that back looks like you can use the cursor orientation contants to select the result... sample code coming... I havent tried this so you may need to play a bit. This is also based on the assumption that a PDO::FETCH_ORI_FIRST acts like a data_seek and leaves the cursor on the first position as opposed to returning it to whatever it was before.

$stmt = $pdo->prepare('SELECT id FROM table', array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
$stmt->execute();

$first = $pdo->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_FIRST);
$first_row = $first['id'];

// other stuff

// first iteration we rewind to the first record;
$cursor = PDO::FETCH_ORI_FIRST;

while (false !== ($row = $stmt->fetch(PDO::FETCH_ASSOC, $cursor))) {
   $id = $row['id'];
   // successive iterations we hit the "next" record
   $cursor = PDO::FETCH_ORI_NEXT; 
   echo $id;
}

<小时>

我不认为你可以倒带一个语句......假设这些块没有被一堆中间逻辑 id 分开,只需在循环中进行即可.


I dont think you can rewind a statement... Assuming these blocks arent seprated by a bunch of intermediary logic id just do it in the loop.

$STH->setFetchMode(PDO::FETCH_COLUMN); // no need to pull an array
$count = 0;
while ($id = $STH->fetch()) {      
  if($count === 0) {
   $first_row = $id;
  }
  echo $id;
  $count++;
}

这篇关于如何在没有 FETCHALL 的情况下在 MYSQL PDO 中获取 2 次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)