Home >Backend Development >PHP Tutorial >How to Migrate from MySQL to MySQLi?
In this article, we will explore how to transition your code from the legacy MySQL API to the improved MySQLi API.
To start the conversion, replace each mysql_* function call with its corresponding mysqli_* counterpart. This approach is recommended if you have existing code based on the procedural MySQL API.
MySQL Function | MySQLi Function |
---|---|
mysql_connect | mysqli_connect |
mysql_error | mysqli_error / mysqli_connect_error |
mysql_query | mysqli_query |
MySQL: You must call mysql_select_db after establishing a connection to specify the target database.
MySQLi: You can specify the database name as the fourth parameter to mysqli_connect or use the mysqli_select_db function for flexibility.
Let's convert part of the provided code:
Original MySQL Code:
$link = mysql_connect($DB['host'], $DB['user'], $DB['pass']) or die("...error handling..."); mysql_select_db($DB['dbName']);
Converted MySQLi Code:
$link = mysqli_connect($DB['host'], $DB['user'], $DB['pass'], $DB['dbName']) or die("...error handling...");
Some functions may have parameter differences. Ensure you review them carefully.
Once the conversion is complete, execute your code to verify that it functions correctly. If not, initiate the debugging process.
The above is the detailed content of How to Migrate from MySQL to MySQLi?. For more information, please follow other related articles on the PHP Chinese website!