The mysql_connect() Trap: Why PHP Won't Call Your Functions
You are encountering a classic symptom of a software ecosystem that has been deprecated for over a decade. The error message Call to undefined function mysql_connect() indicates that your PHP interpreter cannot find the legacy MySQL extension, even though you have seemingly configured everything correctly. This isn't usually a configuration bug; it is an architectural shift in how PHP handles database connections.
The root cause lies in the distinction between the mysql_* functions and their successors. The mysql_* API was removed from the core PHP distribution because it lacked support for prepared statements, making it highly susceptible to SQL injection attacks. Even if you successfully uncomment extension=php_mysql.dll in your php.ini, modern PHP versions (5.5+) no longer load this extension by default, and newer builds often drop it entirely.
However, the error you are seeingâspecifically with PHP 5.2.17 as noted in your environmentâsuggests a mismatch between what Apache expects and what is actually loaded. You mentioned uncommenting both php_mysql.dll and php_mysqli.dll. While mysqli is the non-deprecated successor to the procedural mysql_* functions, simply enabling it does not magically redefine the global mysql_connect() function for backward compatibility unless explicitly configured.
To resolve this without migrating your entire codebase immediately, you must ensure that the mysql extension is actually loaded and that the Apache server has reloaded its configuration to reflect these changes. The log entry File does not exist: C:/Web/favicon.ico appearing alongside your error suggests Apache might be serving static content from a different root than expected, but the fatal error is strictly a PHP loading issue.
If you are running a local development environment (indicated by localhost and the Windows file paths in your logs), check that the php_mysql.dll matches the exact architecture of your PHP build. Mixing 32-bit and 64-bit extensions is a common source of "extension not found" errors on Windows. Ensure the path specified in php.ini points to the DLL located in the same directory as php.exe, or adjust the extension_dir directive accordingly.
Once you have confirmed the extension is loaded, verify this by creating a test script that outputs phpinfo(). Look specifically for the "MySQL Support" section. If it says "enabled," but the function remains undefined, check if there is a conflict with the mysqli extension or a typo in the extension name in your php.ini.
The Modern Approach: Switching to PDO or mysqli
While patching the legacy extension might get you running temporarily, relying on mysql_connect() is not viable for production. The official recommendation from the PHP team has always been to migrate to mysqli (MySQL Improved) or PDO (PHP Data Objects). These APIs support prepared statements and parameter binding, which are essential for security.
If you are on a version of PHP that still supports the legacy extension but you want to avoid the security risks, switching to the procedural mysqli is the most direct path forward without changing your code logic significantly:
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'pasw';
$dbname = 'db';
// Use mysqli_connect instead of mysql_connect
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if (!$conn) {
die('Error connecting to MySQL: ' . mysqli_connect_error());
}
// Select database using the new method
mysqli_select_db($conn, $dbname);
// Always close the connection when done
mysqli_close($conn);
?>
This code replaces the deprecated functions with their improved counterparts. The logic remains identicalâconnect, select databaseâbut the underlying implementation is secure and supported.
Why This Happens in Production
If you plan to deploy this on a live server, note that many Linux distributions (like Ubuntu 14.04+) removed php5-mysql from their repositories entirely because they were using PHP versions where the legacy extension was already disabled by default. You will likely need to install specific packages or build PHP from source if you intend to use mysql_* functions on a production server.
For new projects, or if you are maintaining an older codebase, consider using PDO with a MySQL driver. It offers a unified interface for multiple databases and is the standard in modern PHP frameworks like Laravel. If you are working within the Laravel ecosystem, their official documentation emphasizes using Eloquent ORM or raw queries via PDO rather than direct procedural connections, as seen on https://laravelcompany.com.
Ultimately, fixing the Call to undefined function error requires either loading the specific legacy DLL correctly in your local environment or rewriting your connection logic to use a supported API. The former is a stopgap; the latter is the only sustainable solution for long-term stability and security.