Viewers: in countries Watching now:
Now that PHP has true object-oriented capabilities, it's best practice to access databases using PDO (PHP Data Objects) and MySQLi. These methods produce database-neutral code that works with over a dozen systems, including MySQL, SQL Server, PostgreSQL, and SQLite. Learn how to use PDO and MySQLi to perform basic select, insert, update, and delete operations; improve security with prepared statements; and use transactions to execute multiple queries simultaneously. Author David Powers also covers advanced topics like instantiating custom objects, and compares PDO to MySQLi so you can decide which method is right for you.
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 condition of a conditional statement. So, if not row. You can then display a message. And then everything else needs to be wrapped in an else clause. And the closing curly brace of the else clause needs to go after the display area, which is at the end of the table. We'll put that in a separate PHP block. And we'll add a comment to remind ourselves what it’s for. But if row does contain a result, we need to display that first result before looping through the rest of the result set.
So, we need to get rid of this while loop and change it to a do while loop. So select that while expression, cut it to the clipboard, replace it with do, and then paste the while expression at the end of the loop and follow it with a semicolon. So if row does contain a result, the first result will be displayed then the while loop will go through the rest of the results and display them. So let's test that by saving it, loading it into a browser.
There we are. We've got the full set of results, so that is working fine when we've got results. But we also need to test it to make sure that it works when there are no results. So let's go back and edit the select statement. We'll add in a where clause that we know will produce no results. So where name equals David. And if we save that, refresh the browser. No results found, no errors.
Great. So let's just recap. To check whether a select query returns any results, you need to fetch the first row and store it in a variable. We've done that here. If the result set is empty, the variable will be false. But if it contains a result it will equate to true. So, you can then use a do wild loop to display the first row and any remaining results.
There are currently no FAQs about Accessing Databases with Object-Oriented PHP.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.