-1

I have almost no knowledge in MySQL and I got a problem here:

mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead

It says that the problem is in line number 19 at the file, and that's what I found there:

$link = mysql_connect($AppConfig['db']['host'],$AppConfig['db']['user'],$AppConfig['db']['password']) or die(mysql_error());

My question is how do I fix the code? do I need to change it?

Thank you for reading this, appreciate it.

Omry
  • 1
  • 2
  • This is a not an error, it's an warning. Put `error_reporting(0)` before `mysql_connect`. – Jimmy Jul 27 '15 at 03:01
  • 4
    @Jimmy Worst. Advice. Ever. – Robby Cornelissen Jul 27 '15 at 03:02
  • `mysql_*()` is the interface your code is using to talk to your database. It's old, outdated and no longer maintained. It will be __removed__ from the next version of PHP, due this year, so yes, in time you will need to change your code. In the short term you can disable the message by adding `error_reporting(E_ALL ^ E_DEPRECATED);` at the top of every affected script but I'd be wary of such a blanket fix. –  Jul 27 '15 at 03:03
  • 1
    @Jimmy: please don't post comments like these. There's a reason errors show up and the reason needs to be fixed. Turning off errors is like putting a small piece of paper over a warning light in your car. You didn't fix anything. You just made it look like nothing is wrong...at least until your engine fails. – siride Jul 27 '15 at 03:38

1 Answers1

1

To get rid of the deprecation warning and to safely upgrade to future PHP versions you can use the PDO abstraction layer documented here: http://php.net/manual/en/book.pdo.php

To use PDO we need to install the PHP-MySQL-PDO driver, e.g. in Ubuntu:

apt-get install php5-mysql (provide PHP-Mysql-PDO

You must first create a new instance of a PDO object:

$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';

try {
    $dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

Then prepare a statement to be executed:

$stmt = $dbh->prepare("INSERT INTO REGISTRY (name, value) VALUES (:name, :value)");
$stmt->bindParam(':name', $name);
$stmt->bindParam(':value', $value);

// insert one row
$name = 'one';
$value = 1;
$stmt->execute();
Antonio Bardazzi
  • 2,996
  • 1
  • 23
  • 20
  • 3
    Or use the `mysqli_*` methods. – Robby Cornelissen Jul 27 '15 at 03:03
  • @RobbyCornelissen Judging by the questions we get every day, that's not an overly great alternative. In particular newcomers just slap on an `…i` to every function call, confuse the parameter order, and entirely forgo reading up and adopting parameter binding (which is a chore with mysqli anyway) – mario Jul 27 '15 at 03:58