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?
asked May 16, 2012 at 9:16
7
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
138k22 gold badges57 silver badges95 bronze badges
answered Mar 24, 2016 at 23:45
user3325593user3325593
7895 silver badges5 bronze badges
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
answered Mar 16, 2016 at 8:25
1
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
answered Feb 14, 2013 at 12:19
Nanhe KumarNanhe Kumar
15.3k5 gold badges78 silver badges70 bronze badges
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
answered May 16, 2012 at 9:18
Rohit ChoudharyRohit Choudhary
2,2331 gold badge23 silver badges34 bronze badges
2
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"
answered Apr 19, 2014 at 18:14
Afser2000Afser2000
1641 silver badge7 bronze badges
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.
answered Mar 15, 2018 at 8:05
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
answered May 20, 2020 at 10:11
Christos LytrasChristos Lytras
35.9k4 gold badges77 silver badges110 bronze badges
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
answered Dec 29, 2017 at 4:13
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);
}
}
answered Oct 3, 2017 at 12:00
FlorisFloris
2,6862 gold badges27 silver badges46 bronze badges
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.7k12 gold badges98 silver badges133 bronze badges
answered Jun 10, 2015 at 17:45
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.
answered Dec 26, 2013 at 7:09
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.
answered Aug 1, 2017 at 4:16
Jeff BakerJeff Baker
1,4901 gold badge12 silver badges14 bronze badges
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.
answered Oct 8, 2019 at 15:52
AtulAtul
2,97824 silver badges39 bronze badges
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
answered Apr 3, 2016 at 1:03
This Code Can Help You
<?php
$servername = "localhost";
$username = "root";
$password = "";
$con = new mysqli($servername, $username, $password);
?>
But Change The «servername»,»username» and «password»
answered Sep 2, 2016 at 21:11
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,57212 gold badges30 silver badges39 bronze badges
answered Oct 11, 2017 at 0:45
John Mwaniki / 29 Nov 2021
I have seen it severally where PHP mysqli_connect()
database connection worked perfectly well for months or years, then all of a sudden it stops working. On enabling PHP error reporting, or on checking for errors in the error_log file in the cPanel, you find the error below:
PHP Fatal error: Uncaught Error: Call to undefined function mysqli_connect() in /home/username/public_html/…
This can create a great inconvenience and affect your database-driven website negatively especially if you don’t visit it regularly.
As you can see from the error above, PHP doesn’t recognize the mysqli_connect()
function. This usually happens when some changes are made on the server that affects the mysqli extension.
There are 2 ways of connecting PHP to MySQL database:
- MySQLi extension
- PDO (PHP Data Objects) extension
Earlier versions of PHP(before version 5) used the MySQL extension which became deprecated in 2012 and was replaced with the MySQLi extension. The «i» in MySQLi stands for improved.
MySQLi extension supports both object-oriented and procedural ways of connecting to the database.
Procedural way
$conn = mysqli_connect("hostname", "username", "password", "database");
Object-Oriented way
$conn = new mysqli("hostname", "username", "password", "database");
All you have to do is to replace the values in quotes above with your real database credentials where the hostname in most cases will be «Localhost», though it may differ in some instances. The «username» is the username of the user assigned to the database, «password» is the password of that user, and «database» is the name of the database you are connecting to.
Either of the above methods will enable communication between PHP and the MySQL database server to take place.
When the error above occurs, the first thing you should do is to check if the PHP MySQL extension module is being loaded.
To do so, simply echo the phpinfo()
function. It will show you all the information about your installed PHP version.
<?php
echo phpinfo();
?>
On the PHP information page, scroll down to the section titled «mysqli». You should be able to see the enabled MySQLi library in the Client API library version row as shown below:
There are two libraries for connecting PHP to MySQL database:
- MySQL native driver for PHP (
mysqlnd
) - MySQL Client Library (
libmysql
)
The extensions(mysqli or PDO_MySQL) can either use the mysqlnd or libmysql library to connect from PHP to MySQL.
The MySQL Client Library is a general-purpose client library, meaning it can be used across different languages. On the other hand, mysqlnd library is highly optimized for and tightly integrated into PHP. The mysqlnd library is the default library for PHP 5.4 and later versions. MySQL recommends using the MySQL native driver for PHP (mysqlnd) together with ext/mysqli or PDO_MySQL.
In our case from the above screenshot, you can see that mysqlnd is the enabled driver.
We can now scroll more down to the section titled «mysqlnd» for more information as in the screenshot below:
From the last row(API Extensions) of the above table, you can see that both mysqli
and pdo_mysql
extensions are enabled. This way the database connection works perfectly with no error.
In the case where the database connection results in the undefined function mysqli_connect() error, then you will find that the phpinfo() page doesn’t have the «mysqli» and the «mysqlnd» sections.
The solutions to undefined function mysqli_connect() error
1. Upgrading to a later PHP version
In most of the cases that this has happened, I have found that changing PHP to a later version has always worked and fixed the issue. Let’s say for example your current PHP version is 7.0, you can change to a later PHP version such as PHP 7.4 or 8.0.
This personally fixed the issue for me the last time it happened to one of the websites I manage.
Here is a brief guide on how to check your website PHP version -> 3 Simple ways of checking your website PHP version
And here is a simple guide for upgrading the PHP version -> How to change the PHP version in cPanel
2. Changing mysqli_connect to new mysqli
Try changing your database connection from procedural to OOP, ie. change replace mysqli_connect to new mysqli. Though it is the OOP way, it doesn’t mean that you will have to change anything else in your PHP code. I have seen it severally where making this change fixed the error.
3. Installing the PHP MySQL extension
If you host the website by yourself, you can simply install the php-mysqli extension on Ubuntu by use of the command below:
sudo apt install php-mysqli
You will then be required to restart the apache server with the command below for it to take effect.
sudo service apache2 restart
4. Adding or uncommenting the line «extension=php_mysql.dll» in the php.ini file
If you host the server yourself, open the php.ini file and remove the semicolon in front of the line below:
;extension=php_mysqli.dll
So that it will be like below:
extension=php_mysqli.dll
If this line doesn’t exist, simply add it. After making the changes, save and restart your Apache server.
If your website is hosted by a hosting provider/company, and upgrading to a later PHP version or changing mysqli_connect to new mysqli doesn’t seem to work for you, simply contact the host with the error message and they will fix it for you.
That’s all for this article. It’s my hope that it was helpful for you.
Uncaught error: Call to undefined function mysql_connect()
In this article, we will learn about the uncaught error “Uncaught error: Call to undefined function mysql_connect()”.
This error is encountered when we try to use “mysql_connect()” functions of php5 in php7.
PHP Fatal error: Uncaught Error: Call to undefined function mysql_connect() error is raised because mysql_* functions are completely removed from PHP 7, it previously got deprecated in PHP 5.5, but now it is completely removed.
The older MySQL function are removed due to the following reasons:
- Do not work on Object-Oriented concept
- Won’t support transactions and prepared statements
- Insecure
How to fix Undefined Function Mysql_connect() error
There are four methods to fix undefined function Mysql_connect() error:
- Use MySQLi or PDO
- Connecting to Mysql with the Pdo Object Is Pretty Straight Forward
- Connecting to MySQL with MySqli Connection Object
- Rollback to Older PHP 5, update your code to mysqli or PDO and then upgrade to PHP7
1. Use MySQLi or PDO
mysqli_connect()
Instead of using “mysql_connect()” we should use “mysqli_connect()” in php7 to avoid this error.
Example: $mysql = new mysqli(«localhost»,»root»,»password»,»DB_name»);
PDO(php database objects):
Example:$pdo = new PDO(‘mysql:host=localhost;dbname=database_name ‘, ‘username’, ‘password’);
//pdo requires a valid database to establish connection. If the database is not specified then it throws an exception.
2. Connecting to Mysql with the Pdo Object Is Pretty Straight Forward
$user = 'root'; // Mysql
User$password = ''; // Mysql Password
$server = 'localhost'; // Mysql Host
$database = 'my_database'; // Mysql Databse
// PDO Connection string
$pdo = new PDO("mysql:host=$server;dbname=$database", $user, $password);
3. Connecting to MySQL with MySqli Connection Object
$con = mysqli_connect('localhost', 'username', 'password', 'database');
4. Rollback to Older PHP 5, update your code to mysqli or PDO and then upgrade to PHP7
Best Practice
Use MySQLi wrapper and object mapper with prepared statements.
Example: User PHP-MySQLi-Database-Class https://github.com/ThingEngineer/PHP-MySQLi-Database-Class
By using MySQLi with prepare statement will secure your database connection & in future, if need to upgrade your Database to some other version, you won’t have to update all you mysql connection string in all pages.
This package is free and customizable; you can upgrade by creating your Class & functions.
Webmasters and website owners often upgrade PHP to the latest version to avoid security vulnerabilities. This sometimes causes compatibility errors, and one such error is “PHP fatal error call to undefined function mysql_connect()”.
As a part of our Server Management Services, we help our customers to fix such PHP errors.
Here, let’s discuss how to fix the PHP fatal error call to undefined function mysql_connect()
What is a PHP fatal error?
Using older versions of PHP may expose the website to security vulnerabilities and more importantly, bugs that are fixed in the recent versions.
So it is a better idea to upgrade the PHP version to the latest one. However, it is also important to prevent PHP fatal errors on your website.
PHP Fatal Errors also known as Critical Errors can occur after the PHP upgrade. This error stops/terminates the execution of scripts.
The mysql_connect() PHP fatal error may often occur after upgrading your app to PHP 7+. It will try to use “mysql_connect()” functions of php5 in php7. However, mysql_* functions are completely removed from PHP 7+
Also, Fatal error: Uncaught Error: Call to undefined function mysql_connect() error on WordPress site or dashboard looks like,
How to fix the PHP fatal error call to undefined function mysql_connect() error
After upgrading your PHP version to PHP 7+, there is a chance that you will have the following error:
Fatal error: Uncaught Error: Call to undefined function mysql_connect()
This is due to the removal of the mysql_connect function from PHP 7+ versions.
Let’s see how our Support Engineers fixed the PHP fatal errors
1. Upgrade custom code and WordPress plugins or theme
Initially, we’ll identify whether the site is WordPress or using custom code.
If it is WordPress, the main reason could be compatibility issues. The WordPress theme or plugin may not be compatible with higher PHP versions.
So, we will enable the debug option in wp-config.php and find which WordPress plugin or theme is not compatible with PHP 7+. Then, we will recommend them to upgrade/replace the plugin and theme.
If the site is using custom code, then our developer team will help the customers to make the code compatible with the new PHP version.
This solved the issue.
2. Use MySQLi or PDO
Fatal error: Uncaught Error: Call to undefined function mysql_connect() error can be fixed by the use of MySQLi or PDO.
Many customers are using PHP 7.3 version and WordPress latest version. However, they will get the same error after upgrading.
So, we solved the error by enabling the nd_mysqli extension in the PHP configuration and disabling the mysqli one.
How we fix PHP Fatal error: require_once()
Another issue that we commanly dealing with is PHP Fatal error: require_once(): Failed opening required ‘Mail.php’ (include_path=’.:/usr/share/pear:/usr/share/php’)
Recently, one customer had an issue while sending an email by using PHP mail function with SMTP after upgrading PHP from PHP 5.6 to PHP 7+.
On checking, we have found that the error happened due to an incorrect path to Mail.php. He has created the file mail.php instead of Mail.php and specified as require_once(‘Mail.php’)
So we have renamed the file name to Mail.php and solved the error
[Need assistance to fix PHP Fatal errors? – our Support Engineers will help you.]
Conclusion
In short, php fatal error call to undefined function mysql_connect() error occurs after PHP version upgrade. This is due to the removal of the mysql_connect function from PHP 7+ versions. Today, we saw how our Support Engineers fixed the PHP fatal errors.
PREVENT YOUR SERVER FROM CRASHING!
Never again lose customers to poor server speed! Let us help you.
Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.
GET STARTED
var google_conversion_label = «owonCMyG5nEQ0aD71QM»;
Подскажите, пожалуйста, в чем может быть проблема? Делала по видео уроку.
<?php
$connection = mysql_connect(«localhost», «my user», «123»);
$db = mysql_select_db(«my db»);
mysql_set_chrset(«utf8»);
if(!connection || !db){
exit(mysql_error());
}
$result = mysql_query(» SELECT * FROM news «);
mysql_close();
$row = mysql_fetch_array($result);
echo $row[‘title’];
?>
Выдает ошибку Fatal error: Call to undefined function mysql_connect() in C:xampphtdocsphp.locindex.php on line 11