Call to undefined function mysql_connect()

If you are encountering this error, then it means that you are attempting to use the old mysql_* functions in PHP 7.

The PHP team deprecated this extension in version 5.5.

Then, in PHP 7, they completely removed it altogether.

The error in question will look something like this:

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

In simple terms, this means that the function no longer exists. As a result, you will need to use the mysqli extension or the PDO object instead.

Why did PHP remove the mysql_* functions?

Simply put, they were far too old.

This 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. For example, they do not support transactions or 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, we don’t think that anyone can complain about this.

The extension was officially deprecated in PHP 5.5, which was released back in June of 2013. That was more than nine years ago.

In other words, developers have had nine years to prepare for this.

Fixing the “undefined function mysql_connect” error.

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.

Take a look at the following example:

//Your MySQL user account.
$user = 'root';
//Your MySQL password.
$password = '';
//The server / hostname of your MySQL installation.
$server = 'localhost';
//The name of your MySQL database.
$database = 'my_database';
//Connect using PDO.
$pdo = new PDO("mysql:host=$server;dbname=$database", $user, $password);

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.