Error: The mysql extension is obsolete and will be removed in the future: use mysqli or DOP instead

advertisements

I'm getting this error from a PHP file:

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

Here's a copy of the relevant code:

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

Is there any simple way to switch this to using mysqli to avoid the error?


Use mysqli in this way to avoid notice. Because mysql is deprecated with new versions of php.

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