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

时间:2016-07-24 07:46:43

标签: php mysql sql mysqli

我从PHP文件中收到此错误:

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

这是相关代码的副本:

<?php
$con = mysql_connect("localhost","username","password");
if (!$con) {
  die('Could not connect: ' . mysql_error());
}

mysql_select_db("database_name", $con);

$result = mysql_query("SELECT COUNT(*) FROM `tbltodolist` WHERE status = 'Pending'");
$row = mysql_fetch_array($result);
$total = $row[0];

echo $total;

mysql_close($con);
?>

有没有简单的方法可以将其切换为使用mysqli来避免错误?

2 个答案:

答案 0 :(得分:1)

以这种方式使用mysqli以避免通知。因为新版本的php不推荐使用mysql。

<?php
$con = mysqli_connect("localhost","username","password","database_name");
if (!$con) {
  die('Could not connect: ' . mysqli_error());
}
$result = mysqli_query($con,"SELECT COUNT(*) FROM `tbltodolist` WHERE status = 'Pending'");
$row = mysqli_fetch_array($result);
$total = $row[0];

echo $total;

mysqli_close($con);
?>

答案 1 :(得分:0)

使用 mysqli 代替mysql。更多信息请按照Link或此Link

进行操作