Undefined function mysql_connect()

Undefined function mysql_connect()

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

Stefan Bogdanescu

Founder & Senior Architect

Undefined function mysql_connect()

When you encounter a Fatal error: Call to undefined function mysql_connect() in your PHP application, the immediate instinct is often to check if the MySQL extension is installed. In your specific scenario, you have already executed aptitude install php5-mysql and restarted both MySQL and Apache 2. Furthermore, you verified that /etc/php5/apache2/conf.d/pdo_mysql.ini is being parsed via phpinfo(). While these steps confirm that the pdo_mysql driver and likely the underlying database server are operational, they do not guarantee that the legacy mysql_* extension functions are available to your specific PHP configuration.

This error points directly to a version mismatch or an incomplete installation of the legacy MySQL extension for PHP 5. The function mysql_connect() belongs to the mysql extension (also known as mysqlnd in some contexts, but distinct from PDO). Modern versions of PHP have deprecated this extension entirely because it is not thread-safe and lacks prepared statement support by default. However, if your environment relies on older code expecting these functions, you must ensure the specific package providing them is present.

On Debian-based systems using PHP 5, installing php5-mysql typically installs the PDO driver but may omit the legacy mysql extension depending on the exact version of the package manager and the OS release. You need to verify which PHP modules are actually loaded. Run the following command to list all active extensions:

php -m | grep mysql

If this output only shows pdo_mysql or nothing at all, the legacy extension is missing despite your previous installation attempts. You may need to explicitly install the package named php5-mysql-client or a specific variant like php5-mysqlnd if available in your repository, though it is crucial to note that on newer Debian/Ubuntu releases, the packages for PHP 5 extensions might be renamed or moved to different repositories.

The fact that pdo_mysql.ini is parsed confirms that your PHP configuration directory structure is correct and that Apache is reading the settings files. However, this file only controls the PDO driver, not the procedural mysql extension. The absence of mysql_connect() suggests that the .so file for the legacy extension is either not installed or not located in the extension_dir specified in your php.ini.

If you are maintaining an older codebase that strictly requires these functions, switching to PDO is the recommended long-term strategy. You can replace mysql_connect() with a PDO connection using prepared statements:

$dsn = 'mysql:host=localhost;dbname=validate';
$user = 'your_username';
$password = 'your_password';

$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, $password, $options);
} catch (PDOException $e) {
    // Handle connection error
}

This approach offers significantly better security against SQL injection and ensures compatibility with modern PHP versions. If you must keep using the legacy functions for backward compatibility reasons, ensure that the correct extension package is installed for your exact PHP version (e.g., php5-cli, php5-fpm, or php5-apache) and that the extension=mysql.so directive exists in your active php.ini.

For projects involving Laravel, utilizing a modern ORM like Eloquent eliminates the need for these legacy functions entirely. If you are running an older framework or custom script that depends on mysql_connect(), consider checking if there is a newer package version available in your repository that includes the legacy extension as an optional dependency. Always prioritize migrating to PDO or MySQLi, as relying on the procedural mysql extension becomes increasingly difficult as PHP versions evolve and eventually drop support for it completely.

Our network