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

1

An error occurred in script 'C:\xampp\htdocs\framework\connect.php' on line 13: 
mysql_connect(): The mysql extension is deprecated and will be removed in the future: use       mysqli or PDO instead 
Date/Time: 12-17-2013 15:10:43 

how can i solved this problems. i have look around this maybe the php version problem but still can not delete this error message.

<?php

/* Database config */

$db_host        = 'localhost';
$db_user        = '~';
$db_pass        = '~';
$db_database    = 'banners'; 

/* End config */


$link = @mysql_connect($db_host,$db_user,$db_pass) or die('Unable to establish a DB     connection');

mysql_set_charset('utf8');
mysql_select_db($db_database,$link);

?>

This question is tagged with php mysql

~ Asked on 2013-12-17 14:17:02

The Best Answer is


8

Simply put, you need to rewrite all of your database connections and queries.

You are using mysql_* functions which are now deprecated and will be removed from PHP in the future. So you need to start using MySQLi or PDO instead, just as the error notice warned you.

A basic example of using PDO (without error handling):

<?php
$db = new PDO('mysql:host=localhost;dbname=testdb;charset=utf8', 'username', 'password');
$result = $db->exec("INSERT INTO table(firstname, lastname) VAULES('John', 'Doe')");
$insertId = $db->lastInsertId();
?>

A basic example of using MySQLi (without error handling):

$db = new mysqli($DBServer, $DBUser, $DBPass, $DBName);
$result = $db->query("INSERT INTO table(firstname, lastname) VAULES('John', 'Doe')");

Here's a handy little PDO tutorial to get you started. There are plenty of others, and ones about the PDO alternative, MySQLi.

~ Answered on 2013-12-17 14:28:26


0

?php

/* Database config */

$db_host        = 'localhost';
$db_user        = '~';
$db_pass        = '~';
$db_database    = 'banners'; 

/* End config */


$mysqli = new mysqli($db_host, $db_user, $db_pass, $db_database);
/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

?>

~ Answered on 2013-12-17 14:46:02


Most Viewed Questions: