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.
MySQL Improved uses a single method called query to submit SQL statements to the database. When used with a select query, it returns a MySQLi result object. As well as containing the result set, the result object has a property indicating how many rows it contains. This is MySQLi_num_rows.php, which you can find in the chapter five 05_03 folder of the exercise files.
The try block at the top of the page includes the connection script and then runs a very simple SQL query, a select query. The body of the page contains a table where the results of the query will eventually be displayed. So let's submit the query. To do that, we simply need to call the query method and assign the result to an object. So we'll call the result object result equals db our connection, and then we just use the query method and pass it our SQL, our select query.
To find out how many rows the result set contains, we can access the num_rose property of the result object. So let's insert a new line just before the table is to be displayed. Put it down here on line 23. Scroll up a little bit so we can see things better. I will create a variable called numrows. And then we just use our result object, and call its num_rows property. Num_rows in the property has an underscore.
We can use this value not only to display the number of results, but also to control whether to display the table. So let's create a conditional statement. If not numrows. So if we've got no results, numrows will be 0. We can say that no results were found. But if we have got results, we can say how many there were and then display the table. So we need an else block. And inside the else block, we'll display the values of numrows, so we use a double-quoted string here.
And we need to move this closing curly brace of the else block, delete that from there, and it needs to go right down underneath the table. Now we got a PHP block, which is closing the database connection, so we can add it in there. We are not going to display the results in the table yet. Let's just find out if numrows works, so let's save that and test the page in a browser. Total results found, ten. And there is the table which will be ready to display them later.
But let's amend the select query so that we get no results. So let's go up to the select query and we need to add in a where clause. And I happen to know that the names table does not contain a name David, so we'll put that in there, and we'll save that, return to the browser and refresh. This time, no results found and the table is being hidden. So when you use the query method to submit a select query, the result set is returned as a MySQLi result object, which has a num_rows property that tells you the number of rows that were in the result set.
And you can use this property not only to display how many results there are, but also to control the output if the result is empty. The next step is to display the result.
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.