Php and MSQL simple mixup problem.... help!

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
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Php and MSQL simple mixup problem.... help!

Post by Citizen »

Ok, so I'm trying to run a query that finds the scores from a particular game for a particular user.

This code returns a single value when searching only by the particular game:

Code: Select all

$sql4="SELECT * FROM `arcade_highscores` WHERE `gamename` = '$gameid' ORDER BY `score` DESC LIMIT 1";
	$result4=mysql_query($sql4);
	$row4 = mysql_fetch_array($result4);
	$scorenum = mysql_num_rows($result4);
        echo "$scorenum";
	if($scorenum == 1){
		$yourscore = $row4["score"];
	}
	else {
		$yourscore = "None";
	}
This code returns a single value when searching by the particular user:


Code: Select all

$sql4="SELECT * FROM `arcade_highscores` WHERE `user` = '$user' ORDER BY `score` DESC LIMIT 1";
	$result4=mysql_query($sql4);
	$row4 = mysql_fetch_array($result4);
	$scorenum = mysql_num_rows($result4);
        echo "$scorenum";
	if($scorenum == 1){
		$yourscore = $row4["score"];
	}
	else {
		$yourscore = "None";
	}
But, this code returns no values when I run a query for both at the same time:

Code: Select all

$sql4="SELECT * FROM `arcade_highscores` WHERE `gamename` = '$gameid', `user` = '$user' ORDER BY `score` DESC LIMIT 1";
	$result4=mysql_query($sql4);
	$row4 = mysql_fetch_array($result4);
	$scorenum = mysql_num_rows($result4);
        echo "$scorenum";
	if($scorenum == 1){
		$yourscore = $row4["score"];
	}
	else {
		$yourscore = "None";
	}
Whats going on?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Do the first two queries return the exact same data? The third of those queries is checking against two WHERE clauses (with no AND mind you) which makes the search parameters a tad more strict.
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Post by Citizen »

The first two queries come up with two different results but the third comes up with nothing.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Shoudln't your third query look like this?

Code: Select all

$sql4="SELECT * FROM `arcade_highscores` WHERE `gamename` = '$gameid' AND `user` = '$user' ORDER BY `score` DESC LIMIT 1";
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Citizen wrote:The first two queries come up with two different results but the third comes up with nothing.
The reason that the third query is coming up with nothing (aside from the syntax issue with the AND) is that the first two queries are grabbing different data. Since there is nothing common between the first two query results, when you combine the two for common results, you get none. Does that make sense?
Post Reply