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.
With PDO, you can use the result of a select query to set the properties of an existing PHP object. This is Car.php, which you can find in the chapter four, 04_02 folder of the exercise files. It contains the definition for a custom class called Car. The constructor method takes a single argument, id, and that's used to set the car_id property. The other four properties are set to default values. The class uses the magic set and get methods to set and get the values of properties that don't have their own setter and getter methods.
Then down at the bottom, the definition of the magic toString method simply returns HTML to display the objects' properties and their values. So let's create an instance of the Car class. And to do that, I'm going to use fetch_into.php, which you can also find in the exercise files for this video. The page includes the database connection, then the Car class definition. Then it creates an instance of Car and uses echo to display it.
So let's see what that looks like in a browser. So we've got our object, Car id 10, but the other properties are all their default values. So we need to populate these other properties with values from the database. Let's go back to the editing program. And to save a little bit of time, I'm going to paste in some code that you can find in select.txt in the exercise files for this video. We need to add that in after creating the instance of the car.
I'll paste that in there. This is a prepared statement. It uses a question mark placeholder for car_id, then the statement is prepared and then executed. The value of car_id is passed directly to the execute method. And notice that although only one value is being bound to the anonymous parameter, it has to be an array. So it's array car_id rather than car_id on its own. And we're using the same car_id as we have for the object up here.
So, our prepared statement after it's been executed will have exactly the same properties as the car that we've already created. What we need to do now is to merge the two, as it were. And to use the result of a prepared statement to set the car object's properties, we need to call the setFetchMode method on the statement object. So there's our statement and setFetchMode. And the first argument to that needs to be a PDO constant.
So it's all in uppercase, PDO, then the scope resolution operator, which is two colons and FETCH_INTO, FETCH_INTO, and the second argument is the object that we want to fetch those values into. We've created the object called car, so that's our second argument. Once we've set the fetch mode, we can actually fetch the results to update the car object.
So it's just statement, fetch. So if we save that, go back to the browser and refresh the page, instead of the default values, we've got the values that have been extracted from our database query. And if we go back to the editing program. Say, change that to 18, save and refresh the page. Instead of our Toyota, we get a Citroen and the details for the different car.
So that's how you set the properties of an existing object from a database result set. Let's just look at that again. After executing the statement, we set the fetch mode. And the first argument is the PDO constant, PDO FETCH_INTO. The second argument is the object that you want to set those properties of. However, for this to work, the class definition needs to use the magic set method to set the property values. It won't work with custom setter methods. Otherwise than that, it works fine
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.