In this article, we’re going to explore how you could use a MySQL database to perform CRUD (create, read, update, and delete) operations with PHP. If you want to get your hands dirty with database connectivity in PHP, this article is a great starting point.
If you are just getting started with PHP, you probably realize that database connectivity is an essential feature that you’ll need to get familiar with sooner or later. In most cases, a database is the backbone of any web application and holds the data of the application. So, as a PHP developer, you’ll need to know how to deal with database operations.
In this article, we’ll keep things simple and explore how to use the core mysqli functions. In upcoming articles of this series, we’ll explore a couple of other ways to handle database connectivity.
Today, we’ll go through the basics of database connectivity in PHP, and we’ll use the MySQL database as our database back-end. Let’s have a quick look at what we’ll cover in this article:
I assume that you have a working installation of PHP and MySQL and that you’re aware of the basics of MySQL. Also, you’ll need to make sure that the mysqli extension is enabled in your PHP installation, since we’ll use it to do database operations with the MySQL database.
If you are not sure about the mysqli extension, you can check it using the
phpinfo() function. In the
phpinfo() output, you can check if there’s a section titled mysqli. You should also see the Mysqli Support | enabled header if the mysqli extension is enabled.
There’s also an easy way to check it using the CLI interface. You can run the following command to list all extensions that are enabled with your PHP setup.
It should print a list of extensions, and if it contains the mysqli keyword, the mysqli extension is enabled.
Now that we’re set up, let’s start creating a database connection.
In this section, we’ll discuss how you can establish a database connection using the mysqli extension.
There are two ways you can use the mysqli extension, procedural and object-oriented, but we’ll use the procedural way in this post to keep things simple. If you’re curious about the object-oriented syntax, let me know your questions in the comment section and I’ll be happy to answer them.
Firstly, let’s go through the syntax of the
mysqli_connect function, which is used to set up a connection with the MySQL back-end.
mysqli_connect function takes four arguments and returns the connection object upon successful connection. Let’s go through each argument:
Upon successful connection, the
$connection_obj contains the connection object. The following image shows the various properties on the
With this, you’re ready to run queries against the database which was provided to the
On the other hand, if the connection is not successful, we can debug it as shown in the following snippet:
In the next section, we’ll see how you can select a specific database using the connection object.
In the previous section, we discussed how to set up a database connection using the
mysqli_connect function. In this section, we’ll look at how to select a database once the MySQL connection is successful.
Of course, as we’ve already seen, you can always pass a database in the fourth argument of the
mysqli_connect function itself. But there’s also another way you could do this in case you want to change a database after you’ve connected to MySQL. You can use the
mysqli_select_db function to select a database to work with.
Let’s revise the example discussed in the previous section to see how it can work.
As you can see, we’ve passed only three arguments in the
mysqli_connect function, and that should give us a successful database connection. Instead of passing a database in the fourth argument, we select the database using the
mysqli_select_db function takes two arguments: the connection object and the database you want to connect to.
In the previous sections, we discussed how to set up a MySQL connection and select a database to work with. Now, we’ll look at how to execute different types of queries against the selected database. First, let’s look at how to insert and update records.
If you want to follow along with the examples discussed in this article, you’ll need to create the following MySQL table in your database. It’s the table which we’re going to use in all the examples from now on.
Executing the above command in your MySQL database should create the
employee table as shown in the following image.
To keep things simple, we’ll initialize values that need to be inserted into the employee table at the beginning of the script. However, in most of the cases, these would come from user input in the
$_POST variables submitted using a form.
In the above script, we’ve first initialized the variables that we’re going to use in the insert query. Next, we’ve prepared the insert query and assigned it to the
It’s important to note that we’ve used the
mysqli_real_escape_string function to escape string values that we’re going to use in the insert query. You must use this function when you’re dealing with string values submitted via
$_POST variables. This ensures that your users don’t inject malicious code into your database queries.
Finally, we ran the insert query using the
mysqli_query function. The
mysqli_query function is used to run a query against the active database. To wrap things up, we used the
mysqli_close function to close the active connection.
The result is as follows:
In the previous section, we discussed how you can insert new records in the MySQL table. In this section, we’ll see how you can update a record which already exists in the table.
I assume that a record already exists in the employee table, and the id of that record is 1. In the following example, we’re going to update a record which has a value of 1 in the id column.
As you can see in the above example, we want to update the
phone column of the
employee table where
id is 1. This is very similar to the insert query, except that we’re using
UPDATE instead of
The result is as follows:
So far, we’ve discussed how to insert and update records in the MySQL table. In this section, we’ll see how to retrieve (fetch) records from the MySQL table.
Take a look at the following example.
The first thing that you need to do in order to fetch records from a database is to execute the select query using the
mysqli_query function. Upon successful execution of the select query, the
mysqli_query function returns the
mysqli result object, and that’s what we’ve stored in the
$result variable above.
Next, we iterate over the result set using the
mysqli_fetch_array function in a while loop. The
mysqli_fetch_array function fetches a single row at a time from the mysqli result set.
mysqli_fetch_array function takes two arguments: the result object and result type. The result type indicates what type of array will be returned from the
mysqli_fetch_array function. It could be numeric, associative, or both. It’s an optional argument, and you can pass one of these three values:
MYSQLI_ASSOC indicates that you want to access columns by their name, and
MYSQLI_NUM indicates that you want to access columns by their column number.
If you pass the
MYSQLI_NUM value in the second argument of the
mysqli_fetch_array function, you can access columns as
$row, and so on. In our case, we’ve passed the
MYSQLI_BOTH value, so we can access columns in both ways. If you want to access only associative values, you can use the
mysqli_fetch_assoc function instead.
The result will be this:
In the next and last section, we’ll see how to delete records from a database.
In this section, we’ll see how to delete records from a database.
Take a look at the following example.
As you can see, it works pretty much the same as the insert and update queries. The
mysqli_query function executes the
DELETE query, and that eventually deletes the record from the
In this article, we explored how you can use the MySQL database with PHP. This article was intended to provide you with a basic knowledge of MySQL database connectivity in PHP for beginners. We saw how to go from setting up a database connection to executing different types of queries.