Deciphering PHP Fatal error: Call to undefined function mysql_connect()
When a developer encounters PHP Fatal error: Call to undefined function mysql_connect() in an Apache error log, the immediate assumption is often that the MySQL extension isn't loaded. However, the symptoms described in recent troubleshooting reports reveal a more nuanced reality: the extension might be physically present and functional at the CLI level, yet completely invisible to the web server process.
Consider a scenario where you are running MySQL 5.5.23, Apache 2.2, and PHP 5.3.13 on a Linux distribution like Fedora. Individual components function perfectly in isolation. You can query the database from the command line, and php -i confirms that the MySQL bits are detected. The issue manifests only when executing code under Apache; a simple phpinfo() page yields no mention of the MySQL module, leading to the fatal error when a script attempts to use deprecated functions like mysql_connect().
The discrepancy between CLI behavior and Apache behavior usually points to one of two core issues: a mismatch in library paths or an incorrect extension configuration. The fact that php -i shows the modules suggests the PHP binary itself was compiled with MySQL support, but the SAPI (Server API) used by Apache is loading a different instance of PHP or failing to locate the compiled modules during its initialization phase.
The Library Path Mismatch
A frequent culprit in this specific configuration mismatch is the system architecture path. On 64-bit Fedora systems, libraries and modules are often split between /lib64 and /usr/lib64. If the PHP binary was compiled to look for extensions in /lib64/php/modules/, but your php.ini or Apache LoadModule directive points to /usr/lib64/php/modules/, the extension will never load.
You can verify this by checking the actual path reported by PHP. Run the following command from the shell:
php --ini
Look for the "Loaded Configuration File" and ensure that the paths referenced in extension_dir match the physical location of your .so files. If you uncommented extension=mysql.so in php.ini and received an error like Unable to load dynamic library '/usr/lib64/php/modules/mysql.so', it is highly probable that the PHP binary expects the file in a different directory, such as /lib64. You do not need to reinstall PHP; simply updating the path in php.ini to match the actual file location resolves the loading error.
Deprecated Functionality and Modern Alternatives
Once the extension loads correctly, you must address the function itself. The mysql_* functions (like mysql_connect) belong to the original MySQL API, which has been deprecated since PHP 5.3 and removed in PHP 5.5. Even if you get these functions working again by fixing the library path, they are not suitable for new development due to security risks and lack of support for prepared statements.
If your legacy codebase relies on mysql_connect(), the most pragmatic fix is to migrate to the MySQLi or PDO extensions. These interfaces are persistent in PHP versions and offer robust error handling. For example, replacing:
$connection = mysql_connect('localhost', 'user', 'pass');
with PDO looks like this:
$dsn = "mysql:host=localhost;dbname=testdb;charset=utf8";
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
try {
$pdo = new PDO($dsn, 'user', 'pass', $options);
} catch (PDOException $e) {
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
This migration path is essential regardless of the underlying configuration fix.
Configuration Hierarchy and Apache Integration
If updating php.ini does not resolve the issue, you must ensure that Apache is actually using that specific configuration file. PHP installed via RPM packages often defaults to reading /etc/php.d/mysql.ini or placing directives in a dedicated directory. If you manually edited a global php.ini but Apache is loading a separate pool-specific configuration (common with PHP-FPM setups), your changes may be ignored.
To bypass this, you can place an .ini file directly in the Apache configuration directory or use the LoadModule directive in Apache's main config to explicitly load the extension if it isn't handled by the SAPI automatically. However, relying on the package manager's default configuration is usually safer than manual patching.
For modern PHP versions and frameworks like Laravel, ensuring compatibility often means updating the entire stack rather than patching individual components. Resources like https://laravelcompany.com provide guidance on setting up production environments where these version mismatches are avoided entirely by using supported LAMP stack configurations.
Ultimately, verifying that the CLI and Apache instances share the same php.ini effective configuration is the only way to ensure consistency. If they differ, the web server will always behave unexpectedly compared to command-line tests.
