Al Hennessey
Al Hennessey

Reputation: 2445

Mysql sum not working

i am trying to add up some records from a table, however, it isn't working properly, i.e. when i try var_dump to see what the value is showing i just get string(0) ""

Here is the code

 $current_wealth1 = mysql_query("SELECT SUM(estimated_wealth) as total_house_wealth FROM user_houses WHERE user_id='$user_id'");
var_dump($total_house_wealth);
$current_wealth = $ved_balance + $total_house_wealth;

In terms of output, i am just using conventional php print to output $current_wealth.

Any ideas, what could be the problem?

EDIT: Here is my current code, getting a syntax error on the second line

 $current_wealth1 = mysql_query("SELECT SUM(estimated_wealth) as total_house_wealth FROM user_houses WHERE user_id='$user_id'");
$total_house_wealth = mysql_fetch_array($current_wealth1)[0];
$current_wealth = $ved_balance + $total_house_wealth;

Upvotes: 0

Views: 1271

Answers (3)

dynamic
dynamic

Reputation: 48141

You need of course to do

var_dump(mysql_fetch_array($current_wealth1));

Also most likely you get a syntax error becuase you don't have php 5.4. You can't do ...)[0];

Upvotes: 2

Jamie
Jamie

Reputation: 189

try splitting the fetch array to 2 lines to remove your syntax error?

$current_wealth1 = mysql_query("SELECT SUM(estimated_wealth) as total_house_wealth FROM user_houses WHERE user_id='$user_id'");
$total_house_wealth = mysql_fetch_array($current_wealth1);
$total_house_wealth = $total_house_wealth[0];
$current_wealth = $ved_balance + $total_house_wealth;

also, in your table structure, is user id a character string, or integer? if its an integer, then the userid='$user_id' in the WHERE clause shouldn't be single quoted (e.g. userid=$user_id *although you may want to sanitize the user_id var first*)

Upvotes: 1

bluevector
bluevector

Reputation: 3503

$total_house_wealth isn't declare nor set with any value, so what you are seeing is expected behaviour. What you actually want is a different thing

$total_house_wealth = mysql_fetch_array($current_wealth1)[0];

Upvotes: 1

Related Questions