Will PHP stop sup­port­ing MySQL soon? This is the question that has weighed on the minds of those in the PHP community since an error message started appearing when you connect to a MySQL server via the old mysql extension re­com­mend­ing that you switch to ext/mysqli. This de­prec­a­tion notice has been around since PHP 5.5., while the mysqli extension was first in­tro­duced in PHP 5.0. Just as a reminder, that was back in 2004.

But where did this feeling of un­cer­tainty come from? The PHP team at Oracle must have realised that many were still using ext/mysql, including industry heavy­weights like WordPress. So, Oracle decided to initiate a slow de­prec­a­tion process. However, everything must come to an end at some point, so the mysql extension was removed with the in­tro­duc­tion of PHP 7. In this article, we will introduce the successor MySQLi in detail with examples and discuss what is different between the two ex­ten­sions.

IONOS Cloud Object Storage
Cloud storage at an un­beat­able price

Cost-effective, scalable storage that in­teg­rates into your ap­plic­a­tion scenarios. Protect your data with highly secure servers and in­di­vidu­al access control.

What is MySQLi?

MySQLi is an improved extension (the -i stands for “improved”) of PHP for accessing MySQL databases. MySQL is one of the world’s most popular re­la­tion­al database man­age­ment systems (DBMS) alongside Oracle and Microsoft SQL Server. Re­la­tion­al databases are a core part of the internet because they make it possible to process and store large amounts of data in the long term. This involves splitting up complex data sets into subsets and placing them in relation to one another as required.

Developed in 1994 by the Swedish company MySQL AB, this software is now dis­trib­uted by the Oracle Cor­por­a­tion using a dual licensing system. In addition to the pro­pri­et­ary En­ter­prise Edition, Oracle offers a GPL-licensed open-source edition. This dual licensing allows companies to develop their own ap­plic­a­tions based on MySQL without being bound to a license.

What does the mysqli extension include?

There are three different ways to access a MySQL database. The oldest one uses the MySQL extension, which was de­prec­ated as of PHP 5.5 and fully removed in PHP 7. The mysql() function no longer works in PHP 7. It has been replaced with mysqli().

In addition to the old mysql extension, MySQL databases can also be accessed in PHP using the PHP Data Objects (PDO) extension, which is par­tic­u­larly versatile in its use. The third way uses the MySQL Improved Extension. It has been possible to use the mysqli extension to access MySQL databases as of PHP 5. The following code snippet is an example of PHP MySQLi.

Code snippet: sending an SQL query to a database

The query($sql) method is used to send queries to a database:

<?php
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_errno) {
    die("Connect Error: " . $mysqli->connect_error);
}
$sql = "UPDATE table SET column = 'value' WHERE id = 1";
$mysqli->query($sql);
?>

What are the ad­vant­ages of using MySQLi?

Unlike its pre­de­cessor, the mysqli extension uses both a pro­ced­ur­al and an object-oriented approach. One advantage of object-oriented pro­gram­ming is that once the code has been written, it can be easily main­tained and modified in the future. For example, new classes can be created which inherit prop­er­ties and be­ha­viours from existing classes. This sig­ni­fic­antly shortens de­vel­op­ment time and makes it easier to adapt the program to a changing en­vir­on­ment or new re­quire­ments.

Another major advantage of MySQLi is its use of prepared state­ments. A prepared statement is a statement template for a database system. Unlike normal state­ments, these contain place­hold­ers instead of parameter values. If a statement needs to be executed multiple times (e.g. inside a loop) on a database system with different para­met­ers, using prepared state­ments can increase the speed since the statement is already pre-trans­lated in the database system and only needs to be executed with the new para­met­ers. In addition, prepared state­ments can ef­fect­ively prevent SQL in­jec­tions since the database system verifies the validity of the para­met­ers before they are processed.

Code snippet: prepared state­ments in MySQLi

The following is an example of a prepared statement in MySQLi:

<?php
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_errno) {
    die("Connect Error: " . $mysqli->connect_error);
}
$sql = "UPDATE user SET email = ?, password = ? WHERE id = ?";
$statement = $mysqli->prepare($sql);
$statement->bind_param('ssi', $email, $password, $id);
//Assign values to variables
$id= 1;
$email = "an@example.com";
$password = "new password";
$statement->execute();
?>

Using bind_param(), the para­met­ers in an SQL query are bound to the variables. In the example found above, the first argument found in the bind_param() function is ssi. This argument describes the types of para­met­ers. The argument ssi indicates that there are three para­met­ers in the query: the first type is a string, the second is also a string and the third is an integer. The value d still exists for floating point integers.

After the para­met­ers have been bound to the variables, the cor­res­pond­ing values are assigned to them and the prepared statement is sent to the database using $statement->execute(). Compared to PDO, this is much more com­plic­ated.

Managed Nextcloud from IONOS Cloud
Work together in your own cloud
  • Industry-leading security
  • Com­mu­nic­a­tion and col­lab­or­a­tion tools
  • Hosted and developed in Europe

mysqli() vs. mysql(): why was the PHP function changed?

The switch to MySQLi was un­avoid­able because the old mysql extension was quite simply outdated. Fur­ther­more, backwards com­pat­ib­il­ity was always a priority for the extension which made it difficult to maintain the code. The code dates back to the early days of PHP and MySQL, and was not optimally developed in some respects.

For example, if the con­nec­tion iden­ti­fi­er was not ex­pli­citly defined, all functions would try to use the last one specified. Very unlucky users might even find that mysql_query() accessed a com­pletely different database. The con­nec­tion iden­ti­fi­er was optional in the old function, but it is required in the new extension. In addition, prepared state­ments have been added to make re­triev­ing data from a database table faster and more secure.

Con­veni­ently, many functions can be modified by just adding an -i to the mysql() function. However, there are also some dif­fer­ences between the two ex­ten­sions.

Code snippet: con­nec­tion iden­ti­fi­ers in MySQL and MySQLi

Some mysqli() functions require a con­nec­tion iden­ti­fi­er – a PHP variable created when con­nect­ing to the database. In this example, it is called $link.

<?php
// mysql() to establish a connection:
mysql_connect("localhost", "root", "", "test");
// mysqli() to establish a connection:
$link = mysqli_connect("localhost", "root", "", "test");
?>

Code snippet: re­triev­ing data from a database table

The mysqli_query() function requires a con­nec­tion iden­ti­fi­er; however, the function mysqli_fetch_array() does not.

<?php
$link = mysqli_connect("localhost", "root", "", "test");
// Retrieve data sets:
$datasets = mysqli_query($link,
 "SELECT `name`, 'text', 'date' FROM 'messages'");
// Output data sets:
while (list($name, $text, $date) = mysqli_fetch_array($dataset)) {
echo "<p>$name - $title - $text - $date</p>";
}
?>

In addition to the pre­vi­ously mentioned function, the following functions also require a con­nec­tion iden­ti­fi­er:

Con­clu­sion: MySQLi is faster and more secure

The switch to MySQLi was necessary to improve the speed at which databases could be accessed. Prepared state­ments were in­tro­duced in the new extension which also improve con­nec­tion security since they can prevent SQL in­jec­tions. The database system verifies whether the para­met­ers are valid before pro­cessing them. In addition, the new code is easier to maintain due to its object-oriented approach.

Go to Main Menu