0

I want to save the result of my query (result of sum of all lines for 1 column) in PHP variable.
Example of my table :

CONSO_1 | CONSO_2
  15    |   10
  25    |    2
  54    |   45
  ..    |   ..

I want to sum all lines for CONSO_1 and * 12. Normally my query is OK.

But I want to store the result (here : (15+25+54)*12= 1128) in PHP $variable.

My query :

$query = "Select SUM(CONSO_1) * 12 AS CONSO_Total_1, SUM(CONSO_2) * 25 FROM test WHERE DATE_FORMAT(datetime,'%Y-%m-%d')=DATE(NOW())";

Can you help me please ?

Alexander
  • 3,129
  • 2
  • 19
  • 33
gloops100
  • 159
  • 2
  • 2
  • 11
  • In this case I want to have : $variable = 1128 (= CONSO_Total_1) – gloops100 Feb 23 '14 at 16:07
  • Solved ! :) $result = mysql_query($query); $row = mysql_fetch_array($result); $sumCONSO = $row['CONSO_Total_1']; – gloops100 Feb 23 '14 at 16:22
  • But now I have this error : PHP Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean. This line : $row = mysql_fetch_array($result); – gloops100 Feb 23 '14 at 17:30
  • You providing not enough detalis. Also, there is a [question about your error](http://stackoverflow.com/questions/2973202/mysql-fetch-array-expects-parameter-1-to-be-resource-boolean-given-in-select) – Alexander Feb 23 '14 at 18:19

1 Answers1

1

use this query and store result in a var

$query = "SELECT SUM(CONSO_1) FROM table_name";

This will return the sum of all values from CONSO_1 col.

Kumar V
  • 8,810
  • 9
  • 39
  • 58
napendra
  • 378
  • 4
  • 12