Migrate MySQL to MySQLi in PHP – Hello, welcome back to the complete PHP tutorial series from malasngoding.com. This time we will discuss how to migrate from MySQL to MySQL. MySQLI on PHP.
We created this material considering the many questions from visitors because applications that are done using MySQL will error if run on PHP version 7 which already supports MySQLI.
Migrate MySQL to MySQLi in PHP
Broadly speaking, there is no difference between MySQL and MySQLi. It’s just that this mysql is an old version that is used and it is no longer recommended to develop applications using mysql because there is a possibility that it will be removed in the future.
While MYSQLi (MySQL Improved Extension) is a new version of mysql which has been fixed. Mysqli is made to support the latest features of mysql. The names used in mysqli are also almost the same as those used by mysql.
For example, let’s say we create a connection between the database and php. If using mysql we use the command mysql_connect(). then if using mysqli we just add i in the word mysql more correctly mysqli_connect(). Likewise in the process of calling data from mysql_query() Becomes mysqli_query().
Why did I say that mysql might be deleted in the future, this is because if we look at PHP version 7 – above, mysql can no longer be used. The last php version that can still use mysql as far as I remember sata php version 5.6 and below.
In my opinion, for the reasons above, it is appropriate that we migrate from mysql to mysqli. So what if the project has been done using mysql and wants to be upgraded to mysqli?.
Indeed, now there is still no problem if you use mysql with php version 5 and below. But it’s a good idea to upgrade/migrate from mysql to mysqli. One of the incidents that happened to me was when I installed the project into hosting and got an error because I forgot to downgrade the PHP version.
Therefore, I think we need migrate from mysql to mysqli.
To migrate from mysql to mysqli is actually very easy, there is no difference in particular. Here I will give some examples of migration from mysql ke mysqli :
Creating database connection with php
If using mysql to create a database using mysql_connect then with mysqli we use mysqli_connect. For example:
// menggunakan mysql
|
<strong>// using mysql
$connection = mysql_connect(“localhost”,“root”,“”,“tutorial”); ?> |
menggunakan mysqli
|
<strong>use mysqli</strong>
$connection = mysqli_connect(“localhost”,“root”,“”,“tutorial”); ?> |
Now in the second snippet of the code above, we can see the difference from using mysql to mysqli.
Display data using mysql and mysqli
Another example of migrating from mysql to mysqli is displaying data. If you use mysql the command is mysqli_query(), then if you use mysqi it becomes mysqli_query(). Note the following code:
// menggunakan mysql
|
// using mysql
include “connection.php”; $data= mysql_query(“SELECT * FROM pegawai”)or die(mysql_error()); ?> |
if using mysql, for example, we will call employee data which we will store in the data variable then $data = mysql_query(“continued with the command to display data”). Take a look at the following code that uses mysqli to display data:
// Using mysqli
|
// Using mysqli
include ‘connection.php’; $data = mysqli_query($connection,“SELECT * FROM pegawai”); ?> |
In the code snippet above we can see that the difference between mysql and mysqli is a bit visible, which is in: mysqli_query()and additions $connection before the command displays the data. The $connection variable here is a variable that stores the database connection with php which is in the connection.php file, which we have included first.
Add data using mysql and mysqli
Okay, next we will discuss how to migrate from mysql to mysqli in adding data to php. Note the following code:
|
// using mysql include ‘connection.php’; mysql_query(“INSERT INTO employee VALUES(”,’Amen’,’Jaksel’,’Manager’)”); ?> |
|
// using mysqli include ‘connection.php’; mysqli_query($connection,“INSERT INTO employee VALUES(”,’Amen’,’Jaksel’,’Manager’)”); ?> |
Ok the difference remains in the command mysqli_query() and use of variables $connection exist or not. The following table changes from mysql to mysqli:
MySQL | MySQLi |
mysql_query() | mysqli_query |
mysql_num_rows() | mysqli_num_rows() |
mysql_fetch_array() | mysqli_fetch_array() |
mysqli_close() | mysqli_close() |
mysql_insert_id() | mysqli_insert_id() |
and many others | and many others |
To see direct cases, you can see examples of cases directly from the crud manufacturing process using mysql and mysqli
- Create crud using PHP and MySQL
- Create crud using PHP and MySQLi
The conclusion that we can take from the title of the article on migrating mysql to mysqli in php is:
- Mysqli is a new version of mysql
- To migrate from mysql ke mysqli we just need to change mysql to mysqli.
for example : mysql_num_rows() Becomes mysqli_num_rows(). - If executing a query using mysqli add a variable that stores the php connection with the database, the misql mysqli_query($connection,”–database execution command–“).
- So my friend just needs to replace mysql to mysqli in the way that has been presented above. And it is guaranteed that your application can run on PHP version 7 and above.
OK, that’s it for now Migrate MySQL to MySQLI in PHP we meet again in the next material to complete the complete tutorial series. We really hope for criticism and suggestions for future improvements.
Incoming search terms:
- change mysql script to mysqli
- mysql running in php version
- can mysql 8 use php 5
- can mysqli syntax be used in xamp v 177
- how to downgrade mysqli to mysql
- how to convert mysql script to mysqli
- how to change mysql to mysqli
- how to upgrade mysql to mysqli
- download example PHP 7 and mysql
- using mysql_query 2020