Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
With MySQL Improved, you can create an instance of a custom class and initialize the object's properties with values from a database result. However, you need to be aware that MySQLi sets the objects properties before calling the class's constructor method. The following example shows how it works. This is car.ID, which you can find in the ch07 > 07_05 folder of the exercise files, along with the other files that will be used in this video.
Car.php contains the definition for a custom PHP class called car. The Constructor method takes a single argument, the car's ID, which is then used to set the car ID property. Four other properties are set to default values in the constructor. 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. And down at the bottom, the definition of the magic toString returns HTML that displays the objects properties and their values.
So let's use this to create an instance of the car class. Open mysqli_class.php. This contains the database connection and the definition of the car class. Then it fetches the details of a car using its ID and stores the result as result. To instantiate a custom object from the database result, the result set must be stored as a MySQLi result object. That's why I've used the query method here.
So let's create our custom object, we'll call it car. Then we use the result object and we call it fetch_object_ method. And the first argument to fetch object is a string which is the name of the custom class. The second argument, which is optional, is an array of arguments to be passed to the constructor. In this case, we've got only one argument that needs to be passed. So, we have an array with a single element in it, which is car_ID. The class is magic to string method.
Displays all of the object properties and values so we can use echo to inspect our new object. And if we save that page and load it into a browser. We get the correct car ID but make is unknown, mileage is not registered. And this is the problem with the fetch object method. MySQLi uses the values drawn from the database to set the object's properties and then calls the constructor. As a result, the default values in the constructor overwrite the values from the database, and there's nothing you can do about it other than get rid of default values.
So, if we go back to our code, and to the car definition. Scroll up to the constructor. If we comment out these default properties, and save that page, then go back to the browser and reload. The values from the database are used to populate the property values. Unfortunately, there's currently no way to get the fetch object method to call the constructor first. So, if you're working with objects that have default values in the constructor, you should use PDO as described in chapter 4 rather than MySQLi.
The fetch object method works only with a my SQLI result object. If you're working with a prepared statement, let's just take a little quick look at that. Mysqli_class_stmp.php. This uses a prepared statement. Then on line 18, the get_result method is used to store the result as a MySQLi result object. The fetch object method won't work directly on a statement. So that's how you create a custom object and set it's properties using a database result with MySQLi.
The main limitation is that the fetch object method calls the class constructor after setting the values. As a result, any default values overwrite those drawn from the database.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 100970 Viewers
61 Video lessons · 87754 Viewers
71 Video lessons · 71647 Viewers
56 Video lessons · 103536 Viewers
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.
Your file was successfully uploaded.