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
Indochine
Forum Newbie
Posts: 5 Joined: Tue Jan 27, 2009 5:34 pm
Post
by Indochine » Wed Jan 28, 2009 8:23 am
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!
onion2k
Jedi Mod
Posts: 5263 Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com
Post
by onion2k » Wed Jan 28, 2009 8:26 am
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
Post
by mattpointblank » Wed Jan 28, 2009 8:31 am
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
Post
by jothirajan » Wed Jan 28, 2009 8:35 am
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
Post
by Indochine » Wed Jan 28, 2009 8:51 am
Thanks for the replies.
I see what you mean, but I don't understand how you get a value from the mysql_query.
Thanks.
andyhoneycutt
Forum Contributor
Posts: 468 Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls
Post
by andyhoneycutt » Wed Jan 28, 2009 10:27 am
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
Post
by Indochine » Wed Jan 28, 2009 10:35 am
Many thanks! Works!
Mark Baker
Forum Regular
Posts: 710 Joined: Thu Oct 30, 2008 6:24 pm
Post
by Mark Baker » Wed Jan 28, 2009 11:48 am
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'];