Single Result from Sum with MySQLi

Single Result From SUM With MySQLi

Whatever is given in the SELECT statement to mysqli_query is going to return a mysql_result type if the query was successful. So if you have a SELECT statement such as:

SELECT sum(field) FROM table1

you still need to fetch the row with the result, and the value of the sum() function will be the only entry in the row array:

$res = mysqli_query($dbh,'SELECT sum(field) FROM table1');
$row = mysqli_fetch_row($res);
$sum = $row[0];

Sum of column in using mysqli and php

Try this:

<?php
require('connection.php');

$sql="SELECT sum(amount) as total FROM td";

$result = mysqli_query($sql);

while ($row = mysqli_fetch_assoc($result))
{
echo $row['total'];
}

mysqli_close($con);
?>

As said in a comment above, you don't need the ! infront of the query method.

simple echo SUM and Group by from mysqli

if you group by client_id then you should not select id and date

  $sqlquery = "SELECT  client_id, SUM(item) AS total_sales
FROM test
WHERE date BETWEEN '".$q_billing."' AND '2018-12-31'
GROUP BY client_id ";

show sum result group by date mysqli

I see 2 errors in your query:

  1. You are grouping by order_id when you should be grouping by order_date.
  2. When multiplying values on the query, you should SUM the result of the operation, but you are summing its factors.

That said, your query should be:

SELECT
*,
SUM(order_quantity * order_price) as total
FROM
o_orders_f
GROUP BY
order_date

php mysqli query if i add sum or count it doenst work

If you want relative sumarization, you need to group your data with GROUP BY

SELECT band_naw, SUM(punten) AS punten FROM resultaat GROUP BY band_naw


Related Topics



Leave a reply



Submit