104

I am getting this warning, but the program still runs correctly.

The MySQL code is showing me a message in PHP:

Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in C:\xampp\htdocs\task\media\new\connect.inc.php on line 2

My connect.inc.php page is

<?php
  $connect = mysql_connect('localhost','root','');
  mysql_select_db('dbname');
?>

What does this mean and how can I eliminate the message?

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Mubashar Ahmed Hassan
  • 1,179
  • 3
  • 10
  • 16

12 Answers12

149

There are a few solutions to your problem.

The way with MySQLi would be like this:

<?php
$connection = mysqli_connect('localhost', 'username', 'password', 'database');

To run database queries is also simple and nearly identical with the old way:

<?php
// Old way
mysql_query('CREATE TEMPORARY TABLE `table`', $connection);
// New way
mysqli_query($connection, 'CREATE TEMPORARY TABLE `table`');

Turn off all deprecated warnings including them from mysql_*:

<?php
error_reporting(E_ALL ^ E_DEPRECATED);

The Exact file and line location which needs to be replaced is "/System/Startup.php > line: 2 " error_reporting(E_All); replace with error_reporting(E_ALL ^ E_DEPRECATED);

Tharindu Kumara
  • 4,398
  • 2
  • 28
  • 46
35

You can remove the warning by adding a '@' before the mysql_connect.

@mysql_connect('localhost','root','');

but as the warning is telling you, use mysqli or PDO since the mysql extension will be removed in the future.

Rebirth
  • 1,011
  • 8
  • 14
  • 2
    I see you're not only using bad practices all the way yourself but even telling others to do so – Your Common Sense Apr 07 '14 at 19:11
  • 30
    Hiding the warning is a GOOD THING. It keeps the program going, and mysql_xxxx functions mean no problem right now since they work well in the current version. All you're telling about it being deprecated is PART OF THE ERROR MESSAGE so it's not a wise thing to repeat. Everyone can see it's deprecated, yes, thank you, next please. In the future, mysql function calls will obviously be replaced. Until then, hiding the warning is a practical advice to allow the site to operate while we're silently replacing mysql_xxx calls with something else in the background. Don't be superstitious. – dkellner Aug 04 '14 at 22:54
  • 5
    Incredibly helpful comments in here.. Leave deprecated warnings in and break your live code? Nice.. Of course hiding the warning isn't ideal but it should still be done to keep the code in working order. A good developer will still keep track of deprecated code and schedule it for a future update. – sturrockad Jan 08 '15 at 11:23
  • 1
    error/warning suppression is not how this is fixed - if you have raw warnings/errors being presented to users on your production server, you're doing it wrong - if you only have a production server and no development environment for testing, you're doing it wronger - if you're not fixing errors/warnings in development before releasing to production, you're doing it wrongest – HorusKol Feb 04 '16 at 05:11
  • 1
    @sturrockad A *good developer* would have handled errors better so they never leak into the public and only get logged in the back-end for developers to see. If you're still breaking live code by releasing error messages into the wild, you should probably rethink your entire website. That's not a valid excuse to suppress warnings. – animuson May 26 '17 at 14:28
  • Note that with PHP 7+ this will not work anymore (thus my downvote); it's not a 'solution'... – Gwyneth Llewelyn Sep 24 '19 at 12:04
8

Deprecated features in PHP 5.5.x

The original MySQL extension is now deprecated, and will generate E_DEPRECATED errors when connecting to a database. Instead, use the **MYSQLi or PDO_MySQL extensions.**

Syntax:

<?php
  $connect = mysqli_connect('localhost', 'user', 'password', 'dbname');

Also, replace all mysql_* functions into mysqli_* functions

instead of

<?php
 $connect = mysql_connect('localhost','root','');
  mysql_select_db('dbname');
?> 
Krish R
  • 22,583
  • 7
  • 50
  • 59
7

This warning is displayed because a new extension has appeared. It suppouse that you still can use the old one but in some cases it´s impossible.

I show you how I do the connection with database. You need just change the values of the variables.

My connection file: connection.php

<?php    
 $host='IP or Server Name (usually "localhost") ';
 $user='Database user';
 $password='Database password';
 $db='Database name';

 //PHP 5.4 o earlier (DEPRECATED)
 $con = mysql_connect($host,$user,$password) or exit("Connection Error");
 $connection = mysql_select_db($db, $con);

 //PHP 5.5 (New method)
 $connection =  mysqli_connect($host,$user,$password,$db);
?>

The extension changes too when performing a query.

Query File: "example.php"

<?php
 //First I call for the connection
 require("connection.php");

 // ... Here code if you need do something ...

 $query = "Here the query you are going to perform";

 //QUERY PHP 5.4 o earlier (DEPRECATED)
 $result = mysql_query ($query) or exit("The query could not be performed");

 //QUERY PHP 5.5 (NEW EXTENSION)
 $result = mysqli_query ($query) or exit("The query could not be performed");    
?>

This way is using MySQL Improved Extension, but you can use PDO (PHP Data Objects).

First method can be used only with MySQL databases, but PDO can manage different types of databases.

I'm going to put an example but it´s necessary to say that I only use the first one, so please correct me if there is any error.

My PDO connection file: "PDOconnection.php"

<?php
 $hostDb='mysql:host= "Here IP or Server Name";dbname="Database name" ';
 $user='Database user';
 $password='Database password';

 $connection = new PDO($hostDb, $user, $password);
?>

Query File (PDO): "example.php"

<?php
 $query = "Here the query you are going to perform";
 $result=$connection->$query;
?>

To finish just say that of course you can hide the warning but it´s not a good idea because can help you in future save time if an error happens (all of us knows the theory but if you work a lot of hours sometimes... brain is not there ^^ ).

Ralf Bönning
  • 14,515
  • 5
  • 49
  • 67
jCastellote
  • 79
  • 1
  • 3
4

That is because you are using PHP 5.5 or your webserver would have been upgraded to 5.5.0.

The mysql_* functions has been deprecated as of 5.5.0

enter image description here

Source

Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
3

mysql_*, is officially deprecated as of PHP v5.5.0 and will be removed in the future.

Use mysqli_* function or pdo

Read Oracle Converting to MySQLi

Simone Nigro
  • 4,717
  • 2
  • 37
  • 72
1

Its just a warning that is telling you to start using newer methods of connecting to your db such as pdo objects

http://code.tutsplus.com/tutorials/php-database-access-are-you-doing-it-correctly--net-25338

The manual is here

http://www.php.net/manual/en/book.pdo.php

user1652319
  • 103
  • 1
  • 10
1

Warning "deprecated" in general means that you are trying to use function that is outdated. It doeasnt mean thaqt your code wont work, but you should consider refactoring.

In your case functons mysql_ are deprecated. If you want to know more about that here is good explanation already : Why shouldn't I use mysql_* functions in PHP?

Community
  • 1
  • 1
MSadura
  • 1,032
  • 13
  • 18
1

PDO class replaces these methods. Example for Mysql or MariaDB :

$BDD_SQL = new PDO('mysql:host='.BDD_SQL_SERVER.';dbname='.BDD_SQL_BASE.';charset=utf8', 
        BDD_SQL_LOGIN, BDD_SQL_PWD, 
        array(
          PDO::ATTR_EMULATE_PREPARES => false,
          PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, //launch exception if error
          PDO::ATTR_DEFAULT_FETCH_MODE=> PDO::FETCH_ASSOC
                ));

Source : PDO Class

Karima Rafes
  • 1,016
  • 10
  • 19
0
<?php 
$link = mysqli_connect('localhost','root',''); 
if (!$link) { 
    die('Could not connect to MySQL: ' . mysqli_error()); 
} 
echo 'Connection OK'; mysqli_close($link); 
?>

This will solve your problem.

zubair1024
  • 843
  • 8
  • 24
0

Well, i just faced such message today when i moved to new hosting! anyway i have tried to change the "mySQL" to "mySQLi" but not working, so i have done this:

<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
# Turn off all error reporting
error_reporting(0);
$connect_myconn = "Database Connection";
$hostname_myconn = "localhost";
$database_myconn = "db name";
$username_myconn = "user name";
$password_myconn = "pass";
$myconn = mysql_connect($hostname_myconn, $username_myconn, $password_myconn) or die("<h1 style=margin:0;>A MySQL error has occurred.</h1><p><b>Your Query:</b> " . $connect_myconn . "<br /> <b>Error Number:</b> (" . mysql_errno() . ")</p>" . mysql_error());
mysql_select_db($database_myconn, $myconn);
?>

The trick is to set error reporting off :)

# Turn off all error reporting
error_reporting(0);

For PHP 7+ you can use this code instead:

ini_set('display_errors', 0);
ini_set('log_errors', 1);

Thanks

Mizo Games
  • 189
  • 2
  • 8
  • 1
    Hi, Yes, for PHP 7+ you can use this ```ini_set('display_errors', 0); ini_set('log_errors', 1);```. I'll update the answer above. Thanks. – Mizo Games Sep 25 '19 at 15:42
0

If you have done your coding then

ini_set("error_reporting", E_ALL & ~E_DEPRECATED); 

is good option but if you are in beginning then definitely you should use mysqli.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Manoj Sharma
  • 1,467
  • 2
  • 13
  • 20