Error php fatal error call to undefined function mysql connect

Fatal Error: Uncaught error: Call to undefined function mysql_connect()

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.

Читайте также:  Java convert file type to string

The older MySQL function are removed due to the following reasons:

  1. Do not work on Object-Oriented concept
  2. Won’t support transactions and prepared statements
  3. 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»);

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.

  • Learn PHP Language
  • PHP Interview Questions and Answers
  • PHP Training Tutorials for Beginners
  • Display Pdf/Word Document in Browser Using PHP
  • Call PHP Function from JavaScript
  • Call a JavaScript Function from PHP
  • PHP Pagination
  • Alert Box in PHP
  • Php Count Function
  • PHP Filter_var ()
  • PHP array_push Function
  • strpos in PHP
  • PHP in_array Function
  • PHP strtotime() function
  • PHP array_merge() Function
  • explode() in PHP
  • implode() in PHP
  • PHP array_map()

Источник

[Solved]: Call to undefined function mysqli_connect()

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.

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

Object-Oriented way

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.

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:

phpinfo() mysqli information

There are two libraries for connecting PHP to MySQL database:

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:

mysqlnd information in phpinfo()

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.

Источник

PHP fatal error call to undefined function mysql_connect() – How to fix it

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.

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.

Источник

Оцените статью