mySQL WHERE with MULTIPLE AND's and 1 OR

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
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

mySQL WHERE with MULTIPLE AND's and 1 OR

Post by psychotomus »

Code: Select all

$result = mysql_query("SELECT id FROM pvpchallenges WHERE ((userid='$userid' AND oppid='$usersid' AND mapid='$map->id' AND status='p') OR (userid='$usersid' AND oppid='$userid' AND mapid='$mapid' AND status='p'))") or die(mysql_error());
if(mysql_num_rows($result) == 0)
my statement wont work but I get no errors. all vars are fine. it will always execute my IF statement even after I insert it the first time.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: mySQL WHERE with MULTIPLE AND's and 1 OR

Post by requinix »

What do you get if you actually echo out the query before it runs? Like

Code: Select all

$query = "SELECT id FROM pvpchallenges WHERE ((userid='$userid' AND oppid='$usersid' AND mapid='$map->id' AND status='p') OR (userid='$usersid' AND oppid='$userid' AND mapid='$mapid' AND status='p'))";
echo $query;
$result = mysql_query($query) or die(mysql_error());
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Re: mySQL WHERE with MULTIPLE AND's and 1 OR

Post by psychotomus »

hmm. i get

SELECT id FROM pvpchallenges WHERE ((userid='$userid' AND oppid='$usersid' AND mapid='$map->id' AND status='p') OR (userid='$usersid' AND oppid='$userid' AND mapid='$mapid' AND status='p'))
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: mySQL WHERE with MULTIPLE AND's and 1 OR

Post by superdezign »

Then that is a problem. Are you sure your variables are defined?
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Re: mySQL WHERE with MULTIPLE AND's and 1 OR

Post by psychotomus »

superdezign wrote:Then that is a problem. Are you sure your variables are defined?
100% sure. cause it inserts correct data into mySQL database if its 0 results which is always.
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Re: mySQL WHERE with MULTIPLE AND's and 1 OR

Post by psychotomus »

nvm. had wrong var name. lols ;]
Post Reply