Checking if a SELECT query contains results


show more Checking if a SELECT query contains results provides you with in-depth training on Developer. Taught by David Powers as part of the Accessing Databases with Object-Oriented PHP show less
please wait ...

Checking if a SELECT query contains results

You should check whether a select query returns any results before attempting to display them. If you need to know the actual number, use a select count query as described in the previous video. But, that's not necessary if all you need to know is there's something to display. This is pdo_checkrow.php. You can find it in the chapter 2 > 02_06 folder of the Exercise Files. The code at the top of the page runs a SELECT query and stores the result set as result.

Then a while loop in the body of the page fetches the results and displays them in a table. The way to make sure whether a select query contains any results is to fetch the first row before trying to display it. So we can do that in this PHP block just before the table is displayed. So we'll add a new line on line 23. And then create a variable called row. Then use the result object and call it fetch method. This will get the first row in the results set, but if there are no results, row will be false.

So we can use that as the condit...

Checking if a SELECT query contains results
Video duration: 3m 32s 3h 47m Intermediate

Viewers:

Checking if a SELECT query contains results provides you with in-depth training on Developer. Taught by David Powers as part of the Accessing Databases with Object-Oriented PHP

Subject:
Developer
Software:
PHP
Author:
please wait ...