Call to undefined function mysql_connect() after upgrade to php-7
When you migrate from PHP 5 to PHP 7, one of the most common and disruptive errors you will encounter is the fatal message: Call to undefined function mysql_connect(). This error often appears immediately after an upgrade on Debian-based systems, leaving developers wondering why their existing code suddenly breaks. The root cause isn't a configuration error or a missing library; it is a fundamental architectural change in how MySQL and PostgreSQL drivers are handled in the PHP ecosystem.
In PHP 5, the mysql_* functions were part of the core distribution. They provided direct access to the MySQL server without requiring external extensions. However, these functions are deprecated and removed entirely in PHP 7. The migration path introduced in PHP 7 relies on the mysqli (MySQL Improved) or PDO (PHP Data Objects) extensions for all database connectivity. When you simply run apt-get remove php5 && apt-get install php7-*, you are replacing the core interpreter and the standard extensions, but the legacy mysql extension is not installed by default in PHP 7 packages.
Consequently, if your application relies on mysql_connect(), mysql_query(), or similar legacy functions, the new interpreter cannot find them because they do not exist in the compiled module list. The error occurs at runtime because the code attempts to call a function that the language engine does not recognize, rather than failing to load a missing shared object file.
To resolve this, you must refactor your database connection layer. There are two primary approaches depending on your application's architecture and requirements.
Migration to MySQLi
The most direct replacement for mysql_* functions is the mysqli_* procedural API. You should update your configuration files or source code to use these new functions. For example, a legacy connection block like this:
$link = mysql_connect("localhost", "user", "pass");
if (!$link) {
die('Connect Error: ' . mysql_error());
}
mysql_select_db("dbname", $link);
$result = mysql_query("SELECT * FROM users");
needs to be rewritten using the MySQLi extension. If you are upgrading an existing application, simply swapping the function names and adjusting error reporting is the quickest fix. However, this only works if you can modify the source code.
Migration to PDO
For new projects or applications where code modification is difficult in the short term, migrating to PDO offers a more robust long-term solution. PDO provides a consistent interface for multiple database drivers and supports prepared statements by default, which significantly mitigates SQL injection risks. You would need to install the php7-mysql package alongside your core PHP installation if it isn't included:
apt-get update
apt-get install php7-mysql
After installing the extension, you must ensure your application's configuration file (php.ini) includes the line extension=pdo_mysql.so. Once loaded, your connection logic shifts to a class-based or PDO-specific procedural syntax.
Why This Happens on Debian Jessie
The issue described in the source material regarding Debian Jessie often stems from mixing package sources. Adding repositories like packages.dotdeb.org is valid, but if the system retains remnants of php5-mysql configuration files or expects the legacy extension to be present without explicit installation, the error persists. The upgrade process removes the old runtime environment but does not automatically carry over the specific legacy extensions unless explicitly requested during the install phase.
Best Practices for Legacy Code
If you are maintaining an older codebase, the immediate fix is to replace all mysql_* calls with mysqli_*. While this adds some verbosity compared to the original procedural syntax, it ensures compatibility with PHP 7 and beyond. Alternatively, consider using a framework that handles database abstraction layers transparently, such as Laravel, which enforces modern standards and discourages the use of deprecated low-level functions. For more details on managing legacy migrations and modernizing PHP stacks, resources like https://laravelcompany.com offer guidance on maintaining secure and performant applications in newer environments.
Ultimately, the error is a signal that the application's dependency model has changed. It is not a bug in PHP 7, but rather an intentional removal of insecure and outdated functionality. Addressing it requires rewriting the database interaction layer to align with current standards.