Call to undefined function mysql_connect()

If you are encountering this error, it is probably because you are trying to use the old mysql_* functions in PHP 7. These functions were deprecated in PHP 5.5 before finally being removed altogether in PHP 7.

The error in question will look something like this:

Fatal error: Uncaught Error: Call to undefined function mysql_connect()

Why were the mysql_* functions removed?

This old PHP extension was built for MySQL version 3.23, which was released back in 1999. Besides not providing an OO interface, these functions also fail to support a number of important MySQL features, such as transactions and prepared statements. Instead of trying to maintain this outdated extension, the PHP team decided to wipe the slate clean and completely remove it as of PHP 7.

To be honest, I don’t think that anybody can complain about this. The extension was officially deprecated in PHP 5.5, which was released back in June of 2013. That was over five years ago.

How do I fix this?

You could roll your version of PHP back to an earlier version. However, if you do that, you will be missing out on many of the great things that PHP 7 brings to the table. This includes performance improvements, scalar type hints, return type declarations and security improvements. To name but a few.

By rolling back to an earlier version of PHP, you are just kicking the can down the road.

Instead, you should modify your code so that it uses the PDO object or mysqli instead of the outdated mysql_* functions.

Connecting to MySQL with the PDO object is actually pretty straight forward:

In addition, here are a few PDO tutorials that might help you in your conversion:

Hopefully, you decide to convert to PDO or mysqli instead of taking the lazy approach and reverting to an earlier version.

Comments

comments