PHP Fatal Error Call to undefined function mysql_connect()

Its a brave new world these days with Ubuntu 16.04, PHP 7.0, and so many other new and exciting versions of software.

You’ve probably become frustrated trying to re-install the php mysql libraries or trying to get the mysql module loaded into Apache2. The truth is though, is that mysql_* functions are now defunct. There was still some backwards compatibility up until PHP 7.0, but now you’re forced into using the new libraries MySQLi or PDO_MySQL. Personally, I decided with a current project to transition using the MySQLi library as its the closest analogue to the original mysql library and requires less effort.

Here’s the old function:

resource mysql_connect ([ string $server = ini_get("mysql.default_host") [, string $username = ini_get("mysql.default_user") [, string $password = ini_get("mysql.default_password") [, bool $new_link = false [, int $client_flags = 0 ]]]]] )

Here’s the new function (from the new library):

mysqli mysqli_connect ([ string $host = ini_get("mysqli.default_host") [, string $username = ini_get("mysqli.default_user") [, string $passwd = ini_get("mysqli.default_pw") [, string $dbname = "" [, int $port = ini_get("mysqli.default_port") [, string $socket = ini_get("mysqli.default_socket") ]]]]]] )

Not too bad of a difference, you can almost go ahead and simply update the names of the mysql_* functions you were using and simply use the mysqli_* versions.

However, you’re going to run into a few different problems. For one, many of the functions require the “link” object/reference as the first parameter. So if you did something like $myLink = mysqli_connect(…); You would now need to pass $myLink along as the first parameter to functions like mysqli_select_db() or mysqli_error() and so on.

Of course it would be great if it were just that simple wouldn’t it? Well unfortunately, there is still going to be some legacy code that will throw things for a loop.

For example, if you were using mysql_fetch_array somewhere and were passing along MYSQL_ASSOC for example to have an associative array returned, that’s not going to work any longer as the constant has been deprecated. (Basically must pass along an integer). However, now you can just simply use mysql_fetch_assoc() instead and there’s no need to pass along the type.

Now you can just use the function like so:

array mysqli_fetch_assoc ( mysqli_result $result )

So as you can see. The transition process will be a fairly tame one, but will have you getting a bumpy ride depending on just how many different types of mysql_*() functions you were using.

I know this is not a comprehensive guide, however, there are other articles that have gone further in-depth than I, and would be best suited to check out (for example, by googling “migrate from mysql to mysqli“)

Happy coding!

Belisarius Smith consults as a software engineer, cloud engineer, and security adviser. He has a BSBA in Security Management and is currently completing graduate studies in the Engineering Department at Penn State University with a Masters of Software Engineering. When he isn't traveling, mountain climbing, or reading, he spends his spare time on personal side projects and studies.

Leave a Reply