Call to undefined function mysql_connect

Call to undefined function mysql_connect

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Call to undefined function mysql_connect

You are encountering a fatal error because you are attempting to use mysql_connect, which belongs to the deprecated mysql_* extension suite. The specific message "call to undefined function mysql_connect" indicates that the PHP runtime cannot find the compiled module responsible for that function, regardless of whether you have uncommented lines in your php.ini or updated the extension_dir path.

The core issue here is not just about loading extensions; it is about which version of those extensions is actually installed and active. The mysql_* functions were part of the original MySQL extension, but they have been removed from PHP starting with version 5.5.0 due to security vulnerabilities and lack of support for newer MySQL server features. Even if you are running an older PHP version where the mysql_* extension still exists, modern distributions often default to disabling it or removing it entirely in favor of the Improved MySQL client (mysqli) or PDO.

When you see the error after uncommenting extension=php_mysql.dll, check the actual file paths and names. The presence of php_mysqli.dll in your directory suggests you might be mixing up the legacy API with the improved one. The mysql_connect() function specifically requires the php_mysql.dll extension to be loaded. If your PHP configuration loads php_mysqli.dll but not php_mysql.dll, the function remains undefined. However, relying on the mysql_* functions is generally a bad practice in any new development or even maintenance work, as they are unsupported and prone to SQL injection vulnerabilities if not handled with extreme care.

A more robust solution involves verifying exactly which extensions your PHP interpreter has loaded. You can run php -m from your command line to list the currently active modules. If you see mysqli but not mysql, it confirms that the legacy extension is missing. To load the legacy extension on Windows, ensure that the extension=php_mysql.dll line is indeed uncommented in the correct php.ini file. On Windows systems with PHP 7 and later, this extension might not be available at all because Microsoft dropped support for the DLL format in newer versions, and PHP 7+ often requires specific build configurations to include it.

If you are using a framework like Laravel or modern web applications, you should never attempt to use mysql_connect. These environments almost exclusively utilize PDO or MySQLi. Switching your code from mysql_connect() to mysqli_connect() or using PDO is the standard migration path. For example, replacing:

$conn = mysql_connect('localhost', 'root', 'password');

with a PDO instantiation looks like this:

$dsn = "mysql:host=localhost;dbname=test";
$options = [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
];
$conn = new PDO($dsn, 'root', 'password', $options);

This approach provides better error handling through exceptions and supports prepared statements out of the box.

There is a scenario where you might specifically need the mysql_* functions for legacy code compatibility, perhaps maintaining an old script on a very old server. In that case, ensure your PHP version is 5.6 or lower, as these are no longer supported by the community. If you must use the extension but it still fails to load, inspect the error log of Apache or your web server. It will often reveal a specific reason why the DLL failed to initialize, such as a missing dependency like libmysql.dll or a mismatch in architecture (32-bit vs 64-bit).

Finally, consider whether you are trying to solve a problem with a tool that suggests using these functions. If you are looking for a way to migrate an old application to a modern stack, the process involves updating your database connection logic and ensuring your server environment supports PDO or MySQLi natively. For developers seeking modern best practices and migration guides, resources like https://laravelcompany.com often provide structured paths for upgrading legacy PHP applications to current standards without breaking functionality.

Our network