Fatal error: Call to undefined function mysql_connect()
PhpMysqlPhp Problem Overview
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?
Php Solutions
Solution 1 - Php
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.
Solution 2 - Php
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
Solution 3 - Php
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
Solution 4 - Php
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
Solution 5 - Php
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"
Solution 6 - Php
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.
Solution 7 - Php
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
Solution 8 - Php
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:
- Go to Select PHP Version in CPanel
- Make sure you have PHP 5.x selected
- Make sure
mysql
andmysqli
PHP extensions are checked
Solution 9 - Php
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);
}
}
Solution 10 - Php
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.
Solution 11 - Php
My solution is here (I needed just to remove the last slash (NB: backward slashes) from PHPIniDir 'c:\PHP\'
): https://stackoverflow.com/questions/12221364/fatal-error-call-to-undefined-function-mysql-connect-cannot-solve/18871425#18871425
Solution 12 - Php
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.
Solution 13 - Php
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.
Solution 14 - Php
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.
- Go to the PHP installation folder,
- CHeck for php.ini file, (Only dev, prod file is there, then one of the file as php.ini file)
- Look for "extension=mysqli" and remove the ";" before it.
- Look for "extension_dir" and mentioned the path of "ext" directory.
- Restart the application.
Hope this helps to someone.
Solution 15 - Php
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
Solution 16 - Php
This Code Can Help You
<?php
$servername = "localhost";
$username = "root";
$password = "";
$con = new mysqli($servername, $username, $password);
?>
But Change The "servername","username" and "password"
Solution 17 - Php
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.