简体   繁体   中英

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

New to PHP, trying to figure this out.

$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. If there is an IF statement outside the While, it returns "no results" after the loop is complete. 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?

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';
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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