Jump to content

help with inconsistant mysql query results


Sabmin

Recommended Posts

I'm having a weird issue in which i'm trying to pull rows from a table based on the contents of specific fields.  My query strings are:

 

$ms = ("SELECT * FROM games WHERE game = 'test' AND playername = '$test' OR opponentname = '$test' ORDER BY playdate DESC LIMIT 25") or die ('Error: '.mysql_error ());

 

Which works fine until later I try to use:

 

$ms2 = ("SELECT * FROM games WHERE game = 'test2' AND playername = '$test' OR opponentname = '$test' ORDER BY playdate DESC LIMIT 25") or die ('Error: '.mysql_error ());

 

What is weird is on $ms2 I found if I change it to:

$ms2 = ("SELECT * FROM games WHERE playername = '$test' OR opponentname = '$test' AND game = 'test2' ORDER BY playdate DESC LIMIT 25") or die ('Error: '.mysql_error ());

 

It works... on one page, on the rest neither line works it just pulls every row in the table that fit one of the two name fields.

 

Has anyone experienced anything like this or know why or a fix for this?

 

Thanks!

You must use () in your AND/OR logic to force the order that they are evaluated -

"SELECT * FROM games WHERE game = 'test' AND (playername = '$test' OR opponentname = '$test') ORDER BY playdate DESC LIMIT 25"

 

Also, putting or die(...) on the end of a string assgiment does not make any sense, so I did not bother to include that in the query string.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.