Home >Backend Development >PHP Tutorial >How to Migrate from MySQL to MySQLi?

How to Migrate from MySQL to MySQLi?

Barbara Streisand
Barbara StreisandOriginal
2024-12-29 00:45:14835browse

How to Migrate from MySQL to MySQLi?

Converting MySQL Code to MySQLi

Overview

In this article, we will explore how to transition your code from the legacy MySQL API to the improved MySQLi API.

Procedural 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.

Equivalences

MySQL Function MySQLi Function
mysql_connect mysqli_connect
mysql_error mysqli_error / mysqli_connect_error
mysql_query mysqli_query

Database Selection

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.

Code Sample

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...");

Parameter Differences

Some functions may have parameter differences. Ensure you review them carefully.

Debugging

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn