简体   繁体   English

使用while循环返回PDO结果,如何判断是否没有记录返回?

[英]PDO results returned using a while loop, how do you tell if no records are returned?

New to PHP, trying to figure this out. PHP的新手,试图弄清楚这一点。

$missing_query = $handler->query('SELECT id FROM bla WHERE blee=1 ORDER BY id');
                    while ($missing  = $missing_query ->fetch(PDO::FETCH_OBJ)) {
                    echo $missing->id, ' ';
                    } 

This works fine, but no matter what I try, I can't get it to return "no results" if the While is not completed. 这可以正常工作,但无论我尝试什么,如果While未完成,我都无法返回“无结果”。 If there is an IF statement outside the While, it returns "no results" after the loop is complete. 如果While之外有IF语句,则在循环完成后返回“无结果”。 If there are no loops, putting it inside doesn't work. 如果没有循环,则将其放入内部是行不通的。

What variable will show me that there were no records returned from the FETCH? 什么变量会告诉我FETCH没有返回记录?

Thanks! 谢谢!

EDIT 编辑

This does work, thanks for the reminder to use ==, forgot about that. 确实可以,谢谢您使用==的提示,忘记了。

$querycount = $missing_query ->rowCount();
if ($querycount  == 0) {echo 'No records';}

Is there a better way to accomplish this? 有没有更好的方法可以做到这一点?

Why doesn't 为什么不

if (empty($missing_query)) {echo 'No records';}

Work? 工作?

I figured this out. 我想通了。

        if($missing_query->rowCount()) {
            while ($missing  = $missing_query ->fetch(PDO::FETCH_OBJ)) {
                echo $missing->id, ' ';
            }
        } else {
            echo 'No records';
        }

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

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