I run the PHP query but it does not display result of the sum in MySQL

0

I'm having trouble displaying the following sum.

require_once("config_acesso.php");

$consultar = "SELECT SUM('valor') FROM vendascalc WHERE valor"; 
$resulta = mysqli_query($mysqli,$consultar);

echo "Total a pagar: " . $resulta;

I do not get any results. But in phpMyAdmin the result flows smoothly.

    
asked by anonymous 01.11.2014 / 00:16

2 answers

3

First hit the query, remove the single quotation marks from the valor column, set an alias for the calculated field so that it is more readable to retrieve the values in php, if no nickname is specified, php will assume that 'name 'is the expression used in sum(valor) or 0 (zero) if you use mysqli_fetch_array()

$consultar = "SELECT SUM('valor') FROM vendascalc WHERE valor"; 

Switch to:

$consultar = "SELECT SUM(valor) as total FROM vendascalc WHERE valor"; 

After mysqli_query() retrieve the value of the query with mysqli_fetch_assoc() and loop to get all rows returned by the query.

$resulta = mysqli_query($mysqli, $consultar);

while($item = mysqli_fetch_assoc($resulta)){
   echo $item['total'] .'<br>';
}
    
01.11.2014 / 02:03
-1

Your query is wrong, you need to have a value after WHERE:

SELECT SUM('valor') FROM vendascalc WHERE valor = <algum numero>
    
01.11.2014 / 01:48