Jump to content


Please take a look at this query


2 replies to this topic

#1 Korndawg

    Young Padawan

  • Members
  • Pip
  • 111 posts
  • Gender:Male
  • Location:Texas, USA

Posted 08 April 2008 - 04:16 PM

I know to look on google and MySql's websites for this. I have and they say this will work, however I hate to tell them it's not working on my machine. Please take a look at it and let me know your ideas.
$q = "SELECT * FROM `games` WHERE `sport` = 'Football' && `hometeam` = '$r[name]' OR `visitorteam` = '$r[name]'";

What I am looking for is only games where the sport is football. and the team i sent to it. I know I am getting the right team and everything in the array ($r) because I have it displaying some information above and it works just fine. I believe the problem lies in the && and ||. I have tried all kinds of different orders to put them in (ie && ||, AND ||, && OR, etc.) So can you please tell me exactly how this should be set up.

Thanks in advance!

#2 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 08 April 2008 - 04:47 PM

:yodasearch:
http://www.pixel2lif...showtopic=36853

Don't know about the &&'s, i use AND personally... Though you appear to have tried that already.

Let us know what errors you're getting (ref the above file), and if that doesn't work, does mysql_num_rows() return anything?

Edit: Have you also tried simply echo'ing $q to make sure it read right (copy/paste what it outputs here just in case).

Edit2: AND and OR are not associative (or maybe it's commutative, shows how much attention i paid in the several classes i've taken that have delt with them...). Try putting parentheses around the or statement to make sure everything is being evaluated in the correct order.
SELECT * FROM `games` WHERE `sport` = 'Football' && (`hometeam` = '$r[name]' OR `visitorteam` = '$r[name]')

Edited by rc69, 08 April 2008 - 04:51 PM.


#3 Korndawg

    Young Padawan

  • Members
  • Pip
  • 111 posts
  • Gender:Male
  • Location:Texas, USA

Posted 08 April 2008 - 07:17 PM

rc69, as always you show me how wrong I am.

After I added a few lines, it messed with my old query I had already made (I didn't change variable names) and PHP had no idea what I was talking about. ($r[name] returned blank)

Here is the code I used if anyone for future uses wants to reference it, or anybody wants to show me how to make it cleaner.
$q = "SELECT * FROM `schools` WHERE `id` = '$id'";
$query = mysql_query($q) or die(mysql_error());

...

$q = "SELECT * FROM `games` WHERE `sport` = 'Football' && (`hometeam` = '$row[name]' OR `visitorteam` = '$row[name]')";
$query = mysql_query($q) or die(mysql_error());
while($r=mysql_fetch_array($query)){

...

}






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users