Reputation: 21
<?php
$connection = new mysqli("localhost","root","", "inventory");
$dash_sales_query = "SELECT sum(Total) FROM salesp";
$dash_sales_query_run = mysqli_query($connection,$dash_sales_query) or die(mysqli_error());
if($sales_total = mysqli_num_rows($dash_sales_query_run))
{
echo '<span class="bold">'.$sales_total.'</span>';
}
else
{
echo '<span class="bold">No Data</span>';
}
?>
hi im beginner and i making a analytics for the system in websites. i really want to show the total price of "total (data)". so i sum it as you can see, but it shows only 1. what is my error?? thankyou very much.
Upvotes: 0
Views: 365
Reputation: 31
The function mysqli_num_rows counts the total amount of rows of the query result, but you want to see the result of the query. It shows 1, because mysql is giving you one row with the result.
Try using this:
$result = mysqli_fetch_assoc($dash_sales_query_run);
print_r($result);
Another tip: give your SUM() in the query an alias, so it is easier to read in the result.
Like this: "SELECT SUM(Total) AS total FROM salesp";
Upvotes: 2