如何倒带PDO结果? [英] How can I rewind a pdo result?

查看:48
本文介绍了如何倒带PDO结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道之前曾有人问过这个问题,但是我尝试了Eveything,却找不到它的工作方式. 我有一个准备好的查询,我需要通过两个while()循环使用它两次:

I know this question has been asked before, but I tried eveything and can't find how it works. I have a prepared query, and I need to use it twice with two while() loops like this:

$query = $pdo->prepare('SELECT ...');
$query->execute();
while( $results = $query->fetch() ){
 // instructions
}

// rewind
while( $results = $query->fetch() ){
 // instructions
}

谢谢

推荐答案

就像在第一次迭代中获得所有结果并在第二次循环时一样简单:

Could be as simple as getting all the results on the 1st iteration and loop on the 2nd:

$res = array();
while( $results = $query->fetch() ){
   $res[] = $results;
   // instructions
}

foreach($res as $results){
   // instructions
}

这篇关于如何倒带PDO结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆