Page 1 of 1

Can't get percent to work..

Posted: Wed Jan 28, 2009 8:23 am
by Indochine
Hello!

For some reason it always returns 49, what ever the values of $wins and $losses are.

Code: Select all

 
function returnRank ($username)
        {
            $wins = mysql_query("SELECT `wins` FROM `user_data` WHERE `username` = '$username'");
            $losses = mysql_query("SELECT `losses` FROM `user_data` WHERE `username` = '$username'");
            
            $total = $wins + $losses;
            
            $pc = round(($wins / $total) * 100);
// etc..
}
 
The MySQL queries do work ;P

Any ideas?

Help much appreciated!

Re: Can't get percent to work..

Posted: Wed Jan 28, 2009 8:26 am
by onion2k
mysql_query returns a resource handle, not a value. You need to fetch the record, and then fetch the specific column from that record.

Re: Can't get percent to work..

Posted: Wed Jan 28, 2009 8:31 am
by mattpointblank
Also, you don't need 2 queries to get that data, select both rows at once!

Re: Can't get percent to work..

Posted: Wed Jan 28, 2009 8:35 am
by jothirajan
onion2k wrote:mysql_query returns a resource handle, not a value. You need to fetch the record, and then fetch the specific column from that record.
>>> Absolutely this is

>>> Follow the tips

1) get the specified column you want to have
2) verify by means of using the best syntax ("echo"). if the value comes then assign it to a variable
3) Then do your mathematical work..

Thanks
JOE...

Re: Can't get percent to work..

Posted: Wed Jan 28, 2009 8:51 am
by Indochine
Thanks for the replies.

I see what you mean, but I don't understand how you get a value from the mysql_query.

Thanks.

Re: Can't get percent to work..

Posted: Wed Jan 28, 2009 10:27 am
by andyhoneycutt

Code: Select all

 
function returnRank ($username)
        {
            $result = mysql_query("SELECT `wins`, `losses` FROM `user_data` WHERE `username` = '$username'");
            $row = mysql_fetch_assoc($result);
            $wins = $row['wins'];
            $losses = $row['losses'];
            $total = $wins + $losses;
            $pc = round(($wins / $total) * 100);
// etc..
}
 

Re: Can't get percent to work..

Posted: Wed Jan 28, 2009 10:35 am
by Indochine
Many thanks! Works! :D

Re: Can't get percent to work..

Posted: Wed Jan 28, 2009 11:48 am
by Mark Baker
Or even:

Code: Select all

 
$result = mysql_query("SELECT `wins`/ (`wins` + `losses`) * 100 AS pc FROM `user_data` WHERE `username` = '$username'");
$row = mysql_fetch_assoc($result);
$pc = $row['pc'];