Introduction
Welcome to the third and final part of a series introducing Object Oriented PHP! Please, as I have urged before, go back and read parts one and two if you have not already, the basics are the most important part!
In this Tutorial
Today, we are going to finally complete the task that we’ve been leading up to: writing our MySQLi DB class! We’re going to be using everything we’ve learned so far to do this, so strap in and enjoy!
- Variables and Constructor
- Main Functions
- Some extra goodies
Variables and Constructor
Create a new file called class.db.php
and insert the following:
- /*
- * class db
- * @param Host
- * @param User
- * @param Password
- * @param Name
- */
- class db
- {
- var $host; //MySQL Host
- var $user; //MySQL User
- var $pass; //MySQL Password
- var $name; //MySQL Name
- var $mysqli; //MySQLi Object
- var $last_query; //Last Query Run
- /*
- * Class Constructor
- * Creates a new MySQLi Object
- */
- function __construct($host, $user, $pass, $name)
- {
- $host = $this->host;
- $user = $this->user;
- $pass = $this->pass;
- $name = $this->name;
- $this->mysqli = new mysqli($this->host, $this->user, $this->pass, $this->name);
- }
- }
- $db = new db('localhost', 'root', '', 'blog');
- ?>
**Try and dissect what we have done here before reading the explanation!**
First, we have done the most important part of Object Oriented PHP – organization! We have said what the class name is, and all the parameters that need to be passed to it when it is loaded. Next, inside the class, we have defined our four variables for MySQL connection and then required them as parameters of the constructor function, which can be passed when the class is loaded at the bottom of the file. Next, we have set our $mysqli variable to a new MySQLi Object. Simple, right? Let’s move on.
Main Functions
SELECT
Now that we have the connection down, we can start working with the database. Add a select function like this:
- /*
- * Function Select
- * @param fields
- * @param from
- * @param where
- * @returns Query Result Set
- */
- function select($fields, $from, $where)
- {
- $query = "SELECT " . $fields . " FROM `" . $from . "` WHERE " . $where;
- $result = $this->mysqli->query($query);
- $this->last_query = $query;
- return $result;
- }
Here we have made a select function for selecting data from a MySQL table. We have defined three parameters, and you can see how they fit into the final query. All we return from this function is a query result set, which you can work with however you like in your pages.
INSERT
Of course, we can select data with our class now, but we don’t have anything to select unless we insert it! Let’s do that now by adding this function to your class:
- /*
- * Function Insert
- * @param into
- * @param values
- * @returns boolean
- */
- function insert($into, $values)
- {
- $query = "INSERT INTO " . $into . " VALUES(" . $values . ")";
- $this->last_query = $query;
- if($this->mysqli->query($query))
- {
- return true;
- } else {
- return false;
- }
- }
Another quite simple function, we are just requiring two parameters: the table name to insert the data into, and the values for the fields. Instead of returning any real data for this function, we just go ahead and run it and if it inserted the data, it returns true, and if not, false. Simple for inserting data.
DELETE
Now that we can insert and then select our data, we have to have a way to delete it say if a user wanted to delete their account. Add this function:
- /*
- * Function Delete
- * @param from
- * @param where
- * @returns boolean
- */
- function delete($from, $where)
- {
- $query = "DELETE FROM " . $from . " WHERE " . $where;
- $this->last_query = $query;
- if($this->mysqli->query($query))
- {
- return true;
- } else {
- return false;
- }
- }
- ?>
Some extra goodies
You probably noticed the last_query
variable we defined at the beginning of the class, then we set it every time we ran a query in a function. This is very vital for troubleshooting, to see what’s wrong with your query. Another possible class variable could be a last_error
, that would hold the last error returned.
Conclusion
This will conclude this series on Object Oriented PHP – I hope you’ve at least learned the basics so you’ll be able to power up your applications! Thanks for reading.
This code is obsolete! It's written in PHP 4 which isn't even supported by PHP anymore. It should be written in PHP using private/protected/public instead of var.
It's written in PHP5 - the __construct function doesn't exist in PHP4.
Actually you are both right. However, the post should be updated to use public, protected, or private for variable declarations because that is what is supported in PHP5.