PHP MySQL Connection: OOP Vs. Procedural

Learning to connect to MySQL using the object-oriented method is not as hard as it sounds if you are new to programming or PHP. When I was learning PHP I was presented with the two possible methods, procedural and object-oriented, at the time I had no previous experience with OOP, but I decided to stick with the OOP method since the beginning knowing that I had to learn someday. Let me show those who are new to PHP or haven’t seen or used the OOP method the two ways one can connect to MySQL.

First goes OOP.

@ $db = new mysqli('dblocation','username','password','dbname');

We begin by creating an object with the name of $db, the “@” is the error suppression symbol, this allows us to handle error through exceptions or also with “if” statements.


The procedural way.

@$db = mysqli_connect('dblocation', 'username', 'password', 'dbname');

As you can see, so far the only difference between both methods is that the OO way has the word “new” in front of mysqli while the procedural method adds “_connect” after mysqli. The difference comes when we retrieve or send data.

Let’s query the database

objected-oriented way

$db->query('INSERT INTO table VALUES('username','password'));

We used the database object “$db” we created to call the query function. The query function takes SQL any SQL statement as parameter. If instead of inserting data you want to retrive data using a SELECT statement you must do the following.

// send query to database
$result=$db->query('SELECT * FROM usersTable WHERE userID=1');

$row=$result->fetch_assoc();
// echo user's password
echo $row['password'];
//free result set
$result->free();
//close database connection
$db->close();

The first line of code returns the data, we then store this data in $result. The second line of code fetches an associative array, if we one of our fields in the table “userTable” was named “password” we could, with the thrid line, echo the password of the user whose id=1. The last two lines will close the database connection.

The Procedural Way

If we had chosen to connect to MySQL using the the second method, show at the top, we would insert rows by typing the follwing.

mysqli_query($db, 'INSERT INTO table VALUES('username','password')');

This method is not very different from the other. What we did here is we passed the $db link we created to mysqli_query followed by the query.

To select data using this method do the following.

$result = mysqli_query($db, 'SELECT * FROM usersTable WHERE userID=1')

Again, assuming that we only get one result, we can type the following to create an array from the result.

// send query to database
$row = mysqli_fetch_assoc($result);
// show the user's password
echo $row['password'];
// free result set
mysqli_free_result($result);
// close connection
mysqli_close($db);

Even though the two methods I showed you are not very different from one another I use OOP, not only because that’s the method I learned to use first but also because I believe that it is easier to integrate with classes.Let’s help those who are new to PHP decide, which method do you prefer or already use?