是否可以检查pdostatement :: fetch()是否有结果而无需遍历行? [英] Is it possible to check if pdostatement::fetch() has results without iterating through a row?

查看:135
本文介绍了是否可以检查pdostatement :: fetch()是否有结果而无需遍历行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面需要检查结果,并且我想出的方法是成功的,但是要遍历结果的第一行.有没有一种方法可以不进行重复检查,或者返回到第一行而无需再次执行查询?

I have a page which needs to check for results, and the way I came up with to do it is successful, but iterates through the first row of results. Is there a way I can check without iterating, or to go back to that first row without executing the query again?

我正在这样做:

 $q = pdo::prepare($SQL);
 $q->execute(array(':foo'=> foo, 'bar'=>bar);
 if(!q->fetch){
     //no results;
 }else{
      //results;
 };

它几乎完全达到了我希望的效果,但不幸的是,它跳过了第一行结果.

It does pretty much exactly what I hoped, with the unfortunate side affect of skipping the first row of results.

我不得不再次运行$ q-> execute().有办法避免这样做吗?

I've resorted to running $q->execute() a second time. Is there a way to avoid doing this?

推荐答案

只需将获取的结果放入变量中即可:

Just put the result of fetch into a variable:

if($row = $q->fetch()) {
    // $row contains first fetched row
    echo $row['coloumn_name'];
}
else
    // no results

这篇关于是否可以检查pdostatement :: fetch()是否有结果而无需遍历行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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