mysql_connect():不推荐使用mysql扩展,将来会删除它:使用mysqli或PDO代替

时间:2013-12-17 14:17:03

标签: php mysql

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 

我怎样才能解决这个问题。我看看这可能是php版本的问题,但仍然无法删除此错误消息。

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

?>

2 个答案:

答案 0 :(得分:8)

简而言之,您需要重写所有数据库连接和查询。

您正在使用现已弃用的mysql_*函数,将来将从PHP中删除。因此,您需要开始使用MySQLi或PDO,就像错误通知警告您一样。

使用PDO的基本示例(无错误处理):

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

使用MySQLi的基本示例(无错误处理):

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

这里有handy little PDO tutorial让你入门。还有很多其他的,about the PDO alternative, MySQLi

答案 1 :(得分: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();
}

?>