Home >Backend Development >PHP Tutorial >Why Can't I Mix `mysql_` and `mysqli_` APIs in My PHP Code?
Mixing MySQL APIs in PHP
You have tried mixing the mysql_ and mysqli_ APIs in PHP, as shown in the code examples provided. However, this has resulted in error messages regarding resource incompatibility.
Compatibility Issues
mysql_ and mysqli_ are different APIs with separate implementations. They create resources that are not compatible with each other. Therefore, you cannot mix the two APIs in a single PHP script. Use only one API consistently throughout your application.
Valid Connections
To correctly establish a MySQL connection in PHP, follow the guidelines below:
Confirming Connection Status
To verify if a MySQL connection is valid, use the appropriate mysqli_connect_errno() or mysql_errno() function, depending on the API you are using. For example:
$con = mysqli_connect(...); if (mysqli_connect_errno($con)) { echo "Failed to connect"; } else { echo "Connected"; }
or
$con = mysql_connect(...); if (mysql_errno($con)) { echo "Failed to connect"; } else { echo "Connected"; }
The above is the detailed content of Why Can't I Mix `mysql_` and `mysqli_` APIs in My PHP Code?. For more information, please follow other related articles on the PHP Chinese website!