Home >Backend Development >PHP Tutorial >Why is My PHP Code Throwing a 'Call to Undefined Function mysqli_connect()' Error on the New Server?

Why is My PHP Code Throwing a 'Call to Undefined Function mysqli_connect()' Error on the New Server?

Barbara Streisand
Barbara StreisandOriginal
2024-12-18 05:21:18392browse

Why is My PHP Code Throwing a

Fatal Error: Resolving "Call to Undefined Function mysqli_connect() at New Hosting Destination

Problem Statement:

While migrating an application dealing with reviews to a new hosting site, the developer encountered the error "Fatal error: Call to undefined function mysqli_connect()." This issue did not occur on the original hosting server, despite using identical code that also functioned on a local host.

Code Snippet:

$con = mysqli_connect("","*the_name*","*the_pass*","*the_database*");
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

Cause of the Error:

The error typically indicates that the MySQL extension for PHP is not available on the new hosting site. Without this extension, the mysqli_connect() function cannot be executed.

Solution:

To resolve the error, the PHP-MySQL extension needs to be installed on the new hosting server. Execute the following command in the terminal:

sudo apt install php-mysqli

Rationale:

sudo apt install php-mysqli updates the system database to include the necessary dependencies for running MySQL extension. It retrieves and compiles the extension, making mysqli_connect() and other MySQL functions available for use.

Additional Note:

The solution provided in the question using sudo apt install php7.2-mysqli may not be applicable in all cases, as it specifies a specific PHP version. Using php-mysqli ensures compatibility across different versions of PHP.

The above is the detailed content of Why is My PHP Code Throwing a 'Call to Undefined Function mysqli_connect()' Error on the New Server?. 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