0

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;
1
  • 1
    the function mysql_query doesn't work like that. just read the manual please, and take the RED HINTS about pdo/msqli to heart: php.net/manual/en/function.mysql-query.php Commented Jun 12, 2012 at 20:17

3 Answers 3

2

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];

Sign up to request clarification or add additional context in comments.

Comments

1

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*)

Comments

1

$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];

6 Comments

thanks for the help, this is my first time doing mysql sum, so i didn't really think about fetching the array, although when i tried it, it said i got a syntax error on line 61, which is the line i put you code on. Any ideas?
Put it immediately before var_dump($total_house_wealth); in your original example. Obviously, I can't see your line numbers.
ok i removed the var dump as i don't need it anymore, but now i get the error Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in C:\xampp\htdocs\Arken\profile1.php on line 61 I added my current code in my original post
I didn't see the far right bit of you SQL statement. You have two problems with WHERE user_id='$user_id'. The first is syntactic, it needs to be WHERE user_id='{$user_id}' in order to reference the PHP variable $user_id. The second is SQL injection. You need to mysql_real_escape_string it first: `WHERE user_id = '{mysql_real_escape_string($user_id)}'
okay but if i keep the [0] at the end of the fetch_array, then i still get a syntax error on that line, if i remove it, i get an error in the html.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.