Call to undefined function mysql_connect() 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 badges135 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_connect() php 7

Chuck Le Butt

46.3k59 gold badges191 silver badges281 bronze badges

answered Dec 4, 2015 at 12:29

Call to undefined function mysql_connect() php 7

Abhishek SharmaAbhishek Sharma

6,6791 gold badge13 silver badges20 bronze badges

0

Not the answer you're looking for? Browse other questions tagged php debian php-7 or ask your own question.

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 ()?

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

Does PHP 5.6 support mysql_connect?

Use mysqli_ or PDO. If you get a warning, then you'll have no choice but to switch. It's still available in 5.6.

What is difference between mysql_connect and Mysqli_connect?

Mysqli_connect Vs Mysql_connect? mysqli_* functions are used with a mysqli_connect resource and mysql_* functions are used with a mysql_connect resource. mysqli has more features and is the more current version to use. you may also want to look into PDO which is an OO way of connecting to databases.