Can't get percent to work..

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Indochine
Forum Newbie
Posts: 5
Joined: Tue Jan 27, 2009 5:34 pm

Can't get percent to work..

Post 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!
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Can't get percent to work..

Post 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.
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: Can't get percent to work..

Post by mattpointblank »

Also, you don't need 2 queries to get that data, select both rows at once!
jothirajan
Forum Commoner
Posts: 69
Joined: Tue Jan 27, 2009 12:06 am

Re: Can't get percent to work..

Post 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...
Indochine
Forum Newbie
Posts: 5
Joined: Tue Jan 27, 2009 5:34 pm

Re: Can't get percent to work..

Post 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.
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: Can't get percent to work..

Post 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..
}
 
Indochine
Forum Newbie
Posts: 5
Joined: Tue Jan 27, 2009 5:34 pm

Re: Can't get percent to work..

Post by Indochine »

Many thanks! Works! :D
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Can't get percent to work..

Post 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'];
 
Post Reply