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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.