3
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);

?>
brianc
  • 211
  • 2
  • 3
  • 7

2 Answers2

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.

TheCarver
  • 19,391
  • 25
  • 99
  • 149
  • thanks for your help! i have rewrite my connect.php to mysqli but in my demo.php how can i convert mysql to mysqli? it there any way to do this ? i am a beginner of php – brianc Dec 17 '13 at 14:54
  • @brianc, I don't know what code is inside **demo.php**. Unfortunately, you will just have to read and learn from that tutorial I posted in my answer. There are plenty of other very simple MySQLi tutorials if you search. – TheCarver Dec 17 '13 at 15:04
  • Neither `exec` nor `query` should be used from INSERT operations. You should be using prepared statements. – Dharman Jun 24 '19 at 21:27
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();
}

?>
Anil Meena
  • 903
  • 1
  • 12
  • 28