Fatal error: Call to undefined function mysqli_connect()
When you encounter Fatal error: Call to undefined function mysqli_connect(), it is often a symptom of a missing PHP extension rather than an issue with your database credentials or server configuration syntax. This specific error indicates that the core PHP interpreter cannot locate the mysqli library in its runtime environment. While the code itself might be syntactically correct, the underlying binary that provides the mysqli_connect function has not been loaded.
In the scenario where code functions perfectly on a local development machine but fails immediately upon deployment to a new production server, the culprit is almost invariably the PHP installation on that target server. You cannot simply "copy and paste" PHP files and expect them to behave identically across different FTP servers if the PHP runtime environments differ.
Why the Error Occurs
The mysqli extension is not always bundled with every PHP distribution by default, particularly in older versions or specific minimal installations like those found on some shared hosting providers or lightweight containers. When you run your script on the new server, PHP initializes, loads core functions, and then checks for extensions listed in its configuration (php.ini). If the mysqli module is missing from this list, or if it is compiled as a non-loaded extension, the function remains undefined.
This explains why the error appears only on the new site. On your localhost and the old server, the PHP binary likely had the extension loaded by default. However, the new server's environment is distinct. Even if you copied all application files, the php.ini file or the compiled .so libraries required to make mysqli available were not transferred.
Diagnosing the Missing Extension
To confirm that this is an extension issue rather than a configuration error with your credentials, you need to inspect the active PHP configuration on the failing server. The most reliable way to do this is by running the built-in diagnostic tool:
php -m | grep mysqli
If this command returns nothing, or if mysqli does not appear in the list of loaded modules, the extension is missing. Alternatively, you can inspect the error log located typically at /var/log/php/error.log or within your web server's log directory. Look for messages indicating that a shared object file failed to load, such as:
PHP Warning: PHP Startup: Unable to load dynamic library 'mysqli.so' - trys mysqli, but could not find any of them
This confirms that the system knows about the extension file but cannot map it into memory.
Resolving the Issue
The fix depends entirely on how PHP was installed and managed on the new server. If you are using a package manager like apt (Debian/Ubuntu) or yum (CentOS/RHEL), you must explicitly install the development packages that contain the MySQL interface:
# For Debian/Ubuntu
sudo apt-get install php-mysql
# For CentOS/RHEL
sudo yum install php-mysqlnd
Note that in modern PHP versions, php-mysql is often deprecated in favor of php-mysqlnd, which provides a more secure and performant driver. Ensure you are installing the version compatible with your PHP major release (e.g., php8.1-mysql).
If you are managing the server via Docker or using a container orchestration system, check your Dockerfile or runtime image configuration. You may need to add the -mysqli tag to your base image or install the extension within the container context before your application code is copied in.
Modernizing the Connection Logic
While resolving the missing extension is the immediate priority, it is worth noting that the connection logic shown in the source material relies on passing credentials as separate arguments to mysqli_connect. This method is functionally correct for legacy scripts but does not leverage modern security features available in newer PHP versions.
Once the extension is active and your connection works, consider updating your code to use named parameters or environment variables for secrets. For instance, using a configuration array allows you to pass options like PDO::ATTR_ERRMODE to ensure strict error reporting:
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
];
$dsn = "mysql:host=hostname;dbname=database_name";
$username = getenv('DB_USER');
$password = getenv('DB_PASS');
try {
$pdo = new PDO($dsn, $username, $password, $options);
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
Using the PDO extension allows you to leverage prepared statements by default, which significantly reduces the risk of SQL injection attacks compared to older string concatenation methods often found in legacy codebases. If you are using the Laravel framework or planning a migration toward it, this shift is even more critical, as the laravelcompany.com ecosystem heavily relies on secure database abstraction layers out of the box.
Final Verification
After installing the extension and restarting the PHP-FPM service or Apache web server, clear your browser cache and revisit the application pages. The fatal error should vanish, and your script will proceed to execute the logic defined in your PHP files. Always remember that moving code between environments requires verifying not just file permissions, but also the specific runtime dependencies of the target server.