Fatal error: uncaught error: call to undefined function mysql_error

It is recommended to use either the MySQLi or PDO extensions. It is not recommended to use the old mysql extension for new development, as it was deprecated in PHP 5.5.0 and was removed in PHP 7.

PHP offers three different APIs to connect to MySQL. Below we show the APIs provided by the mysql, mysqli, and PDO extensions. Each code snippet creates a connection to a MySQL server running on "example.com" using the username "username" and the password "password". And a query is run to greet the user.

Example #1 Comparing the three MySQL APIs


I suggest you try out both MySQLi and PDO and find out what API design you prefer.

Read Choosing an API and Why shouldn't I use mysql_* functions in PHP?

← ServerPilot Docs

After upgrading your app to PHP 7+, there is a very slim chance you will receive the following error:

Fatal error: Uncaught Error: Call to undefined function mysql_error[]

This is due to the removal of the mysql_error function from PHP 7+.

If you receive that error, it means some of your app's code, such as a WordPress plugin or theme, is not compatiblele with PHP 7.0.

To resolve this error, you simply need to switch the app to PHP 5.6.

First, open your app's Settings tab in ServerPilot.

Then, click the blue pencil next to your current PHP version.

Select PHP 5.6 from the drop-down list and click Update.

Last updated: January 25, 2017

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:

  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"];

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 //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.

How do I fix uncaught error in PHP?

Solution. Look for the undeclared variables as given in the error. If you are using inbuilt functions, ensure that there is no typo and the correct function is called. Check if the spellings are correct.

What is fatal error call to undefined function?

If you get an error like Fatal error: Call to undefined function mysql_connect[] when trying to install GFI HelpDesk, it probably means that MySQL support has not been enabled for PHP on your server [that is, the PHP module php-mysql has not been installed].

What is uncaught error in PHP?

When an exception is thrown, the code following it will not be executed, and PHP will try to find the matching "catch" block. If an exception is not caught, a fatal error will be issued with an "Uncaught Exception" message.

What is uncaught mysqli_SQL_exception?

Fatal error: uncaught mysqli_SQL_exception: you have an error in your SQL syntax; check the manual that corresponds to your mariadb server version for the right syntax to use.

Chủ Đề