Ready to watch this entire course?
Become a member and get unlimited access to the entire skills library of over 4,900 courses, including more Developer and personalized recommendations.Start Your Free Trial Now
- View Offline
- Connecting to a database with PDO or MySQLi
- Fetching a result set
- Executing simple non-SELECT queries
- Sanitizing user input
- Binding input and output values
- Passing an array of values to the execute() method
- Working with advanced PDO fetch methods
- Executing a MySQLi transaction
- Freeing resources that are no longer needed
- Submitting multiple queries
- Creating an instance of a class from a result set
Skill Level Intermediate
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