Call to undefined function mysql_error php 7

After I upgraded php5 to php7, I get an error 500 with

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

I put this into my apt sources in order to get php7 right now:

deb http://packages.dotdeb.org jessie all
deb-src http://packages.dotdeb.org jessie all

What I basically did is:

apt-get remove php5
apt-get install php7-*

I'm using the current version of Debian Jessie.

But I still get this. There are a lot of questions here on SO and I definitely checked them all out. But I didn't find an answer there yet.

asked Dec 4, 2015 at 12:24

bytecode77bytecode77

13.5k30 gold badges105 silver badges136 bronze badges

7

From the PHP Manual:

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. See also MySQL: choosing an API guide. Alternatives to this function include:

mysqli_connect()

PDO::__construct()

use MySQLi or PDO

Call to undefined function mysql_error php 7

Chuck Le Butt

46.3k59 gold badges192 silver badges281 bronze badges

answered Dec 4, 2015 at 12:29

Call to undefined function mysql_error php 7

Abhishek SharmaAbhishek Sharma

6,6791 gold badge13 silver badges20 bronze badges

0

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

Does PHP 7 support Mysql_connect?

PHP 7 has removed support for the mysql extension and affects the following: Any queries using a mysql_connect function will not function. PHP 7 only allows connections to a MySQL database using mysqli or PDO_MySQL.

What is uncaught error call to undefined function mysql_connect ()?

Fatal Error: 'Call to undefined function mysql_connect()' 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).

How do I fix undefined errors in PHP?

Steps to Resolve the Error of Calling to Undefined Function in PHP.
Verify that the file exists. ... .
Verify that the file has been included using the require (or include ) statement for the above file on the page. ... .
Verify that the file name is spelled correctly in the require statement..

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.