连接数据库时的常见错误

出现了这样的错误 Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in

这样的方法该如何解决呢?

将下面代码改为mysqli或PDO即可。

function connectit () {
global $CFG;
mysql_connect($CFG['db_host'], $CFG['db_user'], $CFG['db_pass']) or die(mysql_error());
mysql_select_db($CFG['db_name']);
}

PDO:

$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
MYSQLI:
$link = mysqli_connect(
 'localhost', /* The host to connect to 连接MySQL地址 */  
 'user',   /* The user to connect as 连接MySQL用户名 */  
 'password', /* The password to use 连接MySQL密码 */  
 'world');  /* The default database to query 连接数据库名称*/  
 
if (!$link) {
  printf("Can't connect to MySQL Server. Errorcode: %s ", mysqli_connect_error());
  exit;
}
   
   
posted @ 2016-06-16 10:35  reason。。  阅读(287)  评论(0编辑  收藏  举报