61

I have set up PHP, MySQL, and Apache. localhost() for PHP and it is working well. But after I downloaded MySQL, it reports:

Fatal error: Call to undefined function mysql_connect()

How can I fix this?

Tirthraj Barot
  • 2,671
  • 2
  • 17
  • 33
pepsicode
  • 685
  • 2
  • 7
  • 10
  • Did you restart apache after you installed the packages? – Devator May 16 '12 at 09:17
  • http://nz.php.net/manual/en/mysql.installation.php – zerkms May 16 '12 at 09:17
  • 1
    You tagged your question [mysql-error-1064] - how do you get that if your script can't communicate with MySQL? – Repox May 16 '12 at 09:20
  • read this article http://www.somacon.com/p109.php , this may help you – Ibrahim Azhar Armar May 16 '12 at 09:20
  • 7
    If mysqli or PDO works then you're better off using them instead anyway. – GordonM May 16 '12 at 09:21
  • 9
    @GordonM is quite right. The `mysql_*` functions [are deprecated](http://www.php.net/manual/en/faq.databases.php#faq.databases.mysql.deprecated). There are [better alternatives](http://www.php.net/manual/en/mysqlinfo.api.choosing.php) that support [parameterized queries](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php). – pilcrow Jul 07 '12 at 14:03
  • What have you tried to debug the problem? – Nico Haase Aug 17 '20 at 14:26

17 Answers17

67

You upgraded to PHP 7, and now mysql_connect is deprecated. Check yours with:

php -version

Change it to mysqli_connect as in:

$host = "127.0.0.1";
$username = "root";
$pass = "foobar";
$con = mysqli_connect($host, $username, $pass, "your_database");

If you're upgrading legacy PHP, now you're faced with the task of upgrading all your mysql_* functions with mysqli_* functions.

Nick
  • 138,499
  • 22
  • 57
  • 95
user3325593
  • 809
  • 6
  • 5
38

If you get this error after upgrading to PHP 7.0, then you are using deprecated libraries.

mysql_connect — Open a connection to a MySQL Server
Warning
This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used.

More here: http://php.net/manual/en/function.mysql-connect.php

Aleksander Rezen
  • 877
  • 7
  • 14
  • 2
    This should be marked as the accepted answer - it's bad practice to still be using mysql_* calls. – Skytiger Jun 06 '17 at 22:22
36

Open your terminal and run bellow command.

sudo apt-get install mysql-server

If you are running PHP you will also need to install the php module for mysql 5:

sudo apt-get install php5-mysql
Nanhe Kumar
  • 15,498
  • 5
  • 79
  • 71
8

Verify that your installation of PHP has been compiled with mysql support. Create a test web page containing <?php phpinfo(); exit(); ?> and load it in your browser. Search the page for MySQL. If you don't see it, you need to recompile PHP with MySQL support, or reinstall a PHP package that has it built-in

Rohit Choudhary
  • 2,253
  • 1
  • 23
  • 34
  • I have similar problem, I don't see any MySQL in my test page: https://jsfiddle.net/ad0gd90y/. Instead I saw only "mysqlnd". Should I install MySQL prior to PHP? How do I recompile PHP with MySQL supprt? @RohitKumarChoudhary – Parveez Ahmed Dec 31 '16 at 15:02
  • Mysqlnd is the native driver of PHP for mysql. Have you installed mysql in your system? – Rohit Choudhary Jan 02 '17 at 05:52
7
PHP.INI

Check if you forgot to enable the options below(loads the modules for mysql among others):

; Directory in which the loadable extensions (modules) reside.
; http://php.net/extension-dir
; extension_dir = "./"
; On windows:
extension_dir = "ext"
Afser2000
  • 164
  • 1
  • 7
6

Use. <?php $con = mysqli_connect('localhost', 'username', 'password', 'database'); ?>

In PHP 7. You probably have PHP 7 in XAMPP. You now have two option: MySQLi and PDO.

6

For CPanel users, if you see this error and you already have PHP 5.x selected for the site, there might be a CPanel update that disabled mysql and mysqli PHP extensions.

To check and enable the extensions:

  1. Go to Select PHP Version in CPanel
  2. Make sure you have PHP 5.x selected
  3. Make sure mysql and mysqli PHP extensions are checked
Christos Lytras
  • 36,310
  • 4
  • 80
  • 113
4

This error is coming only for your PHP version v7.0. you can avoid these using PHP v5.0 else

use it

mysqli_connect("localhost","root","")

i made only mysqli from mysql

Jalpesh Patel
  • 3,150
  • 10
  • 44
  • 68
3

A solution could be to use adapter functions like these to start using mysqli instead of mysql over your entire application:

if (!function_exists('mysql_connect')) {
    function mysql_connect($host = '', $user = '', $password = '', $database = '', $port = 0, $socket = '') {
        return mysqli_connect($host, $user, $password, $database, $port, $socket);
    }
}


if (!function_exists('mysql_select_db')) {
    function mysql_select_db($link, $dbname) {
        mysqli_select_db($link, $dbname);
    }
}
Floris
  • 2,727
  • 2
  • 27
  • 47
2

I had this same problem on RHEL6. It turns out that the mysql.ini file in /etc/php.d only had a module name but needed a full path name. On my RHEL6 system the entry that works is:

; Enable mysql extension module

extension=/usr/lib64/php/modules/mysql.so

After modifying the file, I restarted apache and everything worked.

josliber
  • 43,891
  • 12
  • 98
  • 133
ArtB
  • 21
  • 1
1

My solution is here (I needed just to remove the last slash (NB: backward slashes) from PHPIniDir 'c:\PHP\'): Fatal error: Call to undefined function mysql_connect() cannot solve

Community
  • 1
  • 1
Darius Miliauskas
  • 3,391
  • 4
  • 35
  • 53
1

Am using windows 8 n this issue got resolved by changing the environment variables

follow these steps: Open my computer properties->advanced system settings->Environment variables. Under 'system variables', select 'path' and click on 'edit' In 'variable value', add 'C:\php;' OR the path where php installed.

click OK and apply the settings and restart the system. It should work.

Bhavye
  • 11
  • 1
1

Here is a quick fix:

All the pros will probably hate me for this answer. But I got the same error on a server: Fatal error: Uncaught Error: Call to undefined function mysql_connect() that was using PHP 7. Did not have time to rewrite all the mysql code so a quick, temporary fix if anyone needs it is in CPANEL to look for PHP Configuration and change the version for that account to something like PHP 5.4 instead of PHP 7. Then the code worked fine without the above error.

Jeff Baker
  • 1,492
  • 1
  • 12
  • 15
1

If you are using Windows10, PHP 7.2 and try to connect to mysql. If this error occurred

Uncaught Error: Call to undefined function mysqli() in

The do the following steps to get it correct.

  1. Go to the PHP installation folder,
  2. CHeck for php.ini file, (Only dev, prod file is there, then one of the file as php.ini file)
  3. Look for "extension=mysqli" and remove the ";" before it.
  4. Look for "extension_dir" and mentioned the path of "ext" directory.
  5. Restart the application.

Hope this helps to someone.

Atul
  • 3,043
  • 27
  • 39
0

Check if mysqli module is installed for your PHP version

$ ls /etc/php/7.0/mods-available/mysql*
/etc/php/7.0/mods-available/mysqli.ini /etc/php/7.0/mods-available/mysqlnd.ini

Enable the module

$ sudo phpenmod mysqli
sdaffa23fdsf
  • 297
  • 4
  • 16
0

This Code Can Help You

<?php 

$servername = "localhost";
$username = "root";
$password = "";


$con = new mysqli($servername, $username, $password);

?>

But Change The "servername","username" and "password"

0

The problem shows up when you're using mysql_connect in your code lines so just add it as in mysqli_connect and it will solve the problem.
You also have to then use mysqli throughout your code lines or the problem would surface again.
The example mysql_select_db will then be mysqli_select_db for selecting Database.

Ruli
  • 2,592
  • 12
  • 30
  • 40