Home >Backend Development >PHP Tutorial >Why Am I Getting the 'Fatal Error: Call to Undefined Function mysqli_connect()' in PHP?
Resolving "Fatal Error: Call to Undefined Function mysqli_connect()"
Problem Description:
A user encounters the error "Fatal error: Call to undefined function mysqli_connect()" when attempting to establish a connection to a MySQL database using PHP. The code responsible for connecting to the database appears to be correct and has been tested successfully on localhost and the previous server.
Solution:
The error indicates that the PHP extension for MySQL, "mysqli," is not installed or enabled. To resolve this issue, follow these steps:
Install the mysqli Extension:
Ubuntu/Debian:
sudo apt install php-mysqli
CentOS/Red Hat:
yum install php-mysqli
Restart Apache or Nginx:
Restart your web server to activate the newly installed or enabled extension.
Apache:
sudo systemctl restart apache2
Nginx:
sudo systemctl restart nginx
Verify Installation:
Ensure that the extension has been successfully installed by creating a new PHP file and adding the following code:
<?php if (function_exists('mysqli_connect')) { echo "mysqli_connect function is available"; } else { echo "mysqli_connect function is not available"; } ?>
Run the PHP file to verify the availability of the mysqli_connect function. It should display "mysqli_connect function is available".
By installing and enabling the mysqli extension, the user can establish a connection to the MySQL database and resolve the "Fatal error: Call to undefined function mysqli_connect()" error.
The above is the detailed content of Why Am I Getting the 'Fatal Error: Call to Undefined Function mysqli_connect()' in PHP?. For more information, please follow other related articles on the PHP Chinese website!