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 has five different methods for fetching the results of a select query. The most commonly used is fetch_assoc which gets the next row as an associative array. If you prefer to work with an indexed array, use the fetch_row method. The third method fetch_array offers you a choice. You can have an indexed array, an associative array or both. By default it returns an array that uses both the column name and its number counting from zero as the array keys.
Consequently each value is listed twice. The fetch_all() method returns the complete result set as a multidimensional array, using the column numbers as the array keys for each row. But you also have the option of using the column names, or both. The final method, fetch_object(), returns the row as an object. If you pass in the name of a class it'll create an instance of that class using the current rows values as its properties.
We'll look at fetch_object in chapter seven but let's see the other four methods in action. This is mysqli_fetch_assoc.php. Which you can find in the chapter 5 05_ 04 folder of the exercise files, where you'll also find the other files used in this video. This page contains the finished code from the previous video. So let's use the fetch_assoc method to display the results.
We need to scroll down to between the two table rows. Insert a new line on line 35, add some php blocker in it. Add a while loop. We'll store each row as row. Then we access the result object and call it's fetch assoc method. The closing brace needs to go after the table row, and what this will do is the result object will return each row as an associative array, which will be stored in row.
We can use that to display it in the table cells. So let's add a PHP block in there. A row, and then the column name. The first one that we want is name. And then we can copy that. And replace the non-breaking space in the next two rows. And then just quickly edit the column names. So the second one called name is meaning and the third one is gender. So, if we save that and then view it in a browser.
There are all the results being displayed. So, we are accessing each row using the column names. So let's take a quick look at fetch row. What I'm going to do is I'm going to make a copy of this page. So save as mysqli_fetch_row.php. I then scroll down to the while loop. And instead of calling the fetch assoc method, we'll call the fetch row method. And this will return each row as an indexed array. So we need to make sure which order we're getting the results from in the database. So let's scroll to the top. Make sure name, meaning, and gender are in that order.
We need to count from 0. So name will be 0. Meaning will be 1. And gender will be 2. If we save that, and view it in a browser. We get exactly the same result as before. So the only difference there is whether you prefer to work with an indexed array or an associative array. Let's see how the fetch array method differs. So, if you open, mysqli_fetch_array. This gives an example of how it works. It submits exactly the same query and stores it as a result, and then the code on lines 13 down to 20 gets the first three rows from that result set using fetch array. The first time, on line 14, fetch array is used in its default mode. Then on line 16, a MySQLI constant, who's being passed as an argument to fetch array.
And similarly on line 18, a different MySQLI constant has been passed as an argument. So, let's take a quick look in the browser, to see what the results are. The first row we get both, we get both numbers of columns and names of columns as the indexes. The second one we get the name, in other words, this is an associative array and in the third one we get the column number, so it's an indexed array.
So if we look back again at this page, the code on line 16 is working exactly the same as if we called fetch assoc except it takes more code to do it. And on line 18, is exactly the same as fetch row again. More code. The only reason you might want to use the fetch array method is if you want to use it in its default state where it returns both column numbers and column names. But most of the time I think you'd prefer to use either fetch assoc or fetch row.
Finally, let's take a look at fetch all. So, open mysql_fetch_all.php. This submits exactly the same query and stores it in a result object. And on line 15, the fetch all method is called. This returns a multi-dimensional array of all the results, not one row at a time, but everything together. So let's take a quick look in a browser to see what this returns. And by default, it returns an indexed array. So, a multidimensional indexed array. However, you do have the choice. You can pass either My SQLI_both or My SQLI_assoc as the argument to change the array type. So let's copy both. And just put it in there as the argument. Save that, and refresh the browser. This time, the multidimensional array contains both the column numbers and the column names as the indices.
And. If we change this to MYSQLI_ASSOC and save it, we get a multidimensional associative array. Using the fetch all method can take up a lot of memory because it builds a multidimensional array from the whole result set. But there's rarely any reason to use it because MySQLI let's you rewind a database result for reuse, and we'll cover that next.
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.