Undefined function mysql_connect() [duplicate]
Asked Answered
G

14

112

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.

Glim answered 11/12, 2012 at 17:12 Comment(2)
How about other built in PHP/MySql functions? Do they work?Fort
Perhaps not, as I have tried entering incorrect user/pass and my 'or die' statement is not showing!Glim
L
60

Well, this is your chance! It looks like PDO is ready; use that instead.

Try checking to see if the PHP MySQL extension module is being loaded:

<?php
    phpinfo();
?>

If it's not there, add the following to the php.ini file:

extension=php_mysql.dll
Liquorish answered 11/12, 2012 at 17:13 Comment(5)
Is this a better way to connect to a database? I will look into it but I would really like to get this working first as it worked previously until I moved to a new server.Glim
It is. mysql_* functions are dangerous and deprecated as of PHP 4.1. Check your Apache error logs and restart the server. Does that work?Liquorish
I'm using xampp, after adding that line to php.ini I get the following message in apache error.log Unable to load dynamic library '\\xampp\\php\\ext\\php_mysql.dllEbon
@defmx Does the .dll file exist at that path? If not you need to install it.Liquorish
omg, I would have NEVER figured this out. This line existed in my php.ini file but was commented out. Shouldn't the installer for php_mysql uncomment this line for you?Inapproachable
H
110

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

Hettie answered 17/6, 2016 at 9:15 Comment(3)
Perfect solution for my case, I could not just upgrade yet and break all my existing code. However, the global $con variable in your file was a problem because the included file was not able to read the global variable of the parent class in which constructor I included it. However, I simply created a connection variable inside the functions of the included file where needed. Now my code works fine. Thank you.Animadvert
Thanks .. it saved my life where production was upgraded to php7 and php5 application broke downAnn
This was my problem today when godaddy moved my server. Despite selecting php 5.6 (not 7.0+) and enabling mysql and mysqli options in cpanel, 'mysql_connect' was still undefined. It was still 2 hours of work to diagnose and fix, but your script saved me from making it 8-10 hours! Thank you.Ozan
I
62

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()

Institutionalize answered 11/12, 2012 at 17:31 Comment(3)
I get Unable to locate package php7.0Morman
Replace 7.0 with 7.3Hettie
for me it was : sudo apt-get install php7.4-mysql && sudo service apache2 restartMychal
L
60

Well, this is your chance! It looks like PDO is ready; use that instead.

Try checking to see if the PHP MySQL extension module is being loaded:

<?php
    phpinfo();
?>

If it's not there, add the following to the php.ini file:

extension=php_mysql.dll
Liquorish answered 11/12, 2012 at 17:13 Comment(5)
Is this a better way to connect to a database? I will look into it but I would really like to get this working first as it worked previously until I moved to a new server.Glim
It is. mysql_* functions are dangerous and deprecated as of PHP 4.1. Check your Apache error logs and restart the server. Does that work?Liquorish
I'm using xampp, after adding that line to php.ini I get the following message in apache error.log Unable to load dynamic library '\\xampp\\php\\ext\\php_mysql.dllEbon
@defmx Does the .dll file exist at that path? If not you need to install it.Liquorish
omg, I would have NEVER figured this out. This line existed in my php.ini file but was commented out. Shouldn't the installer for php_mysql uncomment this line for you?Inapproachable
F
59

If someone came here with the problem of docker php official images, type below command inside the docker container.

$ docker-php-ext-install mysql mysqli pdo pdo_mysql

For more information, please refer to the link above How to install more PHP extensions section(But it's a bit difficult for me...).

Or this doc may help you.

https://docs.docker.com/samples/library/php/

Felske answered 4/8, 2016 at 3:7 Comment(1)
mysql is what I wantNarbada
N
20

I was also stuck with the same problem of undefined MySQL_connect().I tried to make changes in PHP.ini file but it was giving me the same error. Then I came to this solution where I changed my code from depreciated php functions to new functions.

$con=mysqli_connect($host,$user,$password);

mysqli_select_db($con,dbname); 
//To select the database

session_start(); //To start the session

$query=mysqli_query($con,your query); 
//made query after establishing connection with database.

I hope this will help you . This solution is correctly working for me .

EDIT:

If you upgrade form old php you need to apt-get install php7.0-mysql

Nieves answered 12/8, 2017 at 11:26 Comment(0)
B
9

Try:

<?php
  phpinfo();
?>

Run the page and search for mysql. If not found, run the following in the shell and restart the Apache server:

sudo apt-get install mysql-server mysql-client php5-mysql

Also make sure you have all the following lines uncommented somewhere in your apache2.conf (or in your conf.d/php.ini) file, from

;extension=php_mysql.so

to

extension=php_mysql.so
Bough answered 11/12, 2012 at 17:39 Comment(0)
F
8

In php.ini file

change this

;extension=php_mysql.dll

into

extension=php_mysql.dll
Footpound answered 20/9, 2016 at 13:18 Comment(0)
W
4

My guess is your PHP installation wasn't compiled with MySQL support.

Check your configure command (php -i | grep mysql). You should see something like '--with-mysql=shared,/usr'.

You can check for complete instructions at http://php.net/manual/en/mysql.installation.php. Although, I would rather go with the solution proposed by @wanovak.

Still, I think you need MySQL support in order to use PDO.

Wheelwork answered 11/12, 2012 at 17:40 Comment(0)
A
3

The question is tagged with ubuntu, but the solution of un-commenting the extension=mysqli.dll is specific to windows. I am confused here?!, anyways, first thing run <? php phpinfo ?> and search for mysql* under Configuration heading. If you don't see such a thing implies you have not installed or enabled php-mysql. So first install php-mysql

sudo apt get install php-mysql

This command will install php-mysql depending on the php you have already installed, so no worries about the version!!.

Then comes the unix specific solution, in the php.ini file un-comment the line

extension=msql.so

verify that msql.so is present in /usr/lib/php/<timestamp_folder>, ELSE

extension=path/to/msql.so

Then finally restart the apache and mysql services, and you should now see the mysql section under Configrations heading in phpinfo page

Amphiboly answered 9/12, 2016 at 16:48 Comment(0)
L
0

I was getting this error because the project I was working on was developed on php 5.6 and after install, the project was unable to run on php7.1.

Just for anyone that uses Vagrant with ubuntu/nginx, in the nginx directory(/etc/nginx/), there is a directory named "sites-available" which contains a file named like the url configured for the vagrant maschine. In my case was homestead.app. Within this file there is a line that says something like

    fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;

There you can change the php version to the desired for that particular site.

Googled this but wasnt really able to find a simple answer that said where to look and what to change.

Hope that this helps anyone. Thanks.

Lombardo answered 9/8, 2017 at 19:16 Comment(0)
H
-1

If you are getting the error as

Fatal error: Call to undefined function mysql_connect()

Kindly login to the cPanel >> Click on Select Php version >> select the extension MYSQL

Handler answered 4/5, 2016 at 8:19 Comment(1)
There is no suggestion in the question that the OP is using CPanel.Hypoacidity
P
-1

For CentOS 7.8 & PHP 7.3

yum install rh-php73-php-mysqlnd

And then restart apache/php.

Piaffe answered 7/10, 2020 at 21:32 Comment(1)
Please share more details. Are you sure this adds mysql_connect() to PHP?Orangy
T
-3

(Windows mysql config)

Step 1 : Go To Apache Control Panel > Apache > Config > PHP.ini

Step 2 : Search in Notepad (Ctrl+F) For: ;extension_dir = "" (could be commented with a ;). Replace this line with: extension_dir = "C:\php\ext" (please do not you need to remove the ; on the beginning of the sentence).

Step 3 : Search For: extension=php_mysql.dll and remove the ; in the beginning.

Step 4 : Save and Restart You Apache HTTP Server. (On Windows this usually done via a UI)

That's it :)

If you get errors about missing php_mysql.dll you'll probably need to download this file from either the php.net site or the pecl.php.net. (Please be causius about where you get it from)

More info on PHP: Installation of extensions on Windows - Manual

Tengler answered 24/4, 2016 at 14:29 Comment(3)
While this might be correct for Windows, this question is tagged Ubuntu, which most certainly doesn't have C:\\php on the system.Titled
@Titled doesn't matter if that's for Ubuntu, it has worked for me today after a fresh installation on windows.Blade
@RaviGauravPandey The original question pertained to Ubuntu. That it works for you on Windows isn't the point. This is a Windows answer to an Ubuntu question.Titled
I
-4

There must be some syntax error. Copy/paste this code and see if it works:

<?php
    $link = mysql_connect('localhost', 'root', '');
    if (!$link) {
        die('Could not connect:' . mysql_error());
    }
    echo 'Connected successfully';
    ?

I had the same error message. It turns out I was using the msql_connect() function instead of mysql_connect().

Irruptive answered 11/7, 2015 at 18:10 Comment(1)
OP has undefined function mysql_connect and not msql_connect so he didn't type it incorrectly as you suggest.Hamitosemitic

© 2022 - 2024 — McMap. All rights reserved.