i am trying to echo the result of the SQL SELECT COUNT but it seems doesn't work for me, can anyone of you help me? The error show that
mysql_fetch_array expects parameter 1 to be resources, boolean given ....
This is my sql
<?php
$query = ("SELECT COUNT(*) AS total FROM `errors` WHERE `sta` = 0");
$result = mysql_query($query);
$count = mysql_fetch_array($result);
echo $count['total'];
?>
Try to following code:
$query = ($con, "SELECT COUNT(*) AS total FROM errors WHERE sta = 0");
$count = mysqli_fetch_assoc($query);
echo $count['total'];
Hope will help you
You would use mysql_result() like shown below to retrieve the output as an int.
$query = mysql_query("SELECT COUNT(*) FROM `errors` WHERE `sta` = 0");
$result = mysql_result($query, 0, 0);
echo $result;
mysql_* extension is deprecated as of PHP 5.5.0, and has been removed as of PHP 7.0.0. Instead, either the mysqli or PDO_MySQL extension should be used.
Instead of doing this, you can change your query and directly use mysql_num_rows
as it also gives the count. And I guess you only need the count so it is better way, and here is the code :
<?php
$query = ("SELECT * FROM `errors` WHERE `sta` = 0");
$result = mysql_query($query);
$row = mysql_fetch_array($result);
$count = mysql_num_rows($result);
echo $count; // this will give the total count
?>
// try this code...
$query = "SELECT * FROM `errors` WHERE `sta`='0'";
$result = mysql_query($query) or die (mysql_error());
$row2 = mysql_fetch_array($result);
$count= mysql_num_rows($result);
echo $count;