Undefined function mysql_connect()
I have ran aptitude install php5-mysql
(and restarted MySQL/Apache 2), but I am still getting this error:
Fatal error: Call to undefined function mysql_connect() in /home/validate.php on line 21
phpinfo()
says the /etc/php5/apache2/conf.d/pdo_mysql.ini file has been parsed.
Solution 1:
In case, you are using PHP7 already, the formerly deprecated functions mysql_*
were removed entirely, so you should update your code using the PDO-functions or mysqli_*
functions instead.
If that's not possible, as a workaround, I created a small PHP include file, that recreates the old mysql_*
functions with mysqli_*()
-functions: fix_mysql.inc.php
Solution 2:
I see that you tagged this with Ubuntu. Most likely the MySQL driver (and possibly MySQL) is not installed. Assuming you have SSH or terminal access and sudo permissions, log into the server and run this:
sudo apt-get install mysql-server mysql-client php5-mysql
If the MySQL packages or the php5-mysql package are already installed, this will update them.
UPDATE
Since this answer still gets the occasional click I am going to update it to include PHP 7. PHP 7 requires a different package for MySQL so you will want to use a different argument for the apt-get command.
# Replace 7.4 with your version of PHP
sudo apt-get install mysql-server mysql-common php7.4 php7.4-mysql
And importantly, mysql_connect()
has been deprecated since PHP v5.5.0. Refer the official documentation here: PHP: mysql_connect()