Jump to content

Passing query sum result to php variable


GFOnyX

Recommended Posts

I have the following query that gives me the result i want. In a nutshell it searches two columns to find the max value in each of these two columns and returns the sum of these two values.

 

When i try to pass it to a variable in php it doesn't seem to work. I want to be able to pass the sum in a variable so i can use the variable later into an IF Condition.

For example:   if ($scoremax >= '160') { code here }

 

Here is my query:

select (max(scoreioa) + max(scoreioa2)) from result where username='username'

and here is the php code:

$maxscore = mysql_query("select (max(scoreioa) + max(scoreioa2)) from result where username='username'");
mysql_data_seek($maxscore, $scoremax);
$row = mysql_fetch_row($maxscore);
$scoremax = $row[0];

dumb question.. are you sure you are properly connected to the db and all that? 

 

do this:

$maxscore = mysql_query("select (max(scoreioa) + max(scoreioa2)) from result where username='username'") or die(mysql_error());
$row = mysql_fetch_row($maxscore);
echo "<pre>";print_r($row); echo "</pre>";
$scoremax = $row[0];
echo $scoremax;

do you get any errors displayed? what does the print_r show? what is echo'd out? is 'username' really the username? 

Yes i am propelry connected to the DB since i can already pass scores to the DB and read data from it.

 

Sorry about the username,it was a mistake from my end.It was supposed to be $username.

 

The result however is still that is not working.

 

By applying the code you wrote i get this:

Array(    [0] => )

.josh it seems to be working.Thank you very much.

 

I had the code written in wrong position inside the php file. It has many lines of code already in it and i got a little lost. I tried an IF condition using the $scoremax variable and i got the results i wanted.

 

Thanks again!

 

BTW This site is amazing!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.