Jump to content

Passing query sum result to php variable


GFOnyX
Go to solution Solved by .josh,

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];
Edited by GFOnyX
Link to comment
Share on other sites

  • Solution

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? 

Link to comment
Share on other sites

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] => )
Link to comment
Share on other sites

.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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.