Jump to content

Simple query problem involving Boolean logic.


Mavent

Recommended Posts

I'm building a query that searches by database and returns matching (or almost matching) terms.  That part isn't the problem- I have it up and working.  The problem is that I'm trying to narrow down the search results, and it's not working.

 

Here's the query that works:

$result = mysql_query("SELECT * FROM auctions WHERE  name LIKE '%".$searchterm."%' OR Address LIKE '%".$searchterm."%' OR state like '%".$searchterm."%'");

 

Here's the query that DOESN'T works:

$result = mysql_query("SELECT * FROM auctions WHERE type='Cars' AND  name LIKE '%".$searchterm."%' OR Address LIKE '%".$searchterm."%' OR state like '%".$searchterm."%'");

 

What I'm trying to do is say "give me all the results from type:Cars.  Instead, it ignores the WHERE type='Cars' statement, and returns results for all types.  It frustrates me because I use the same exact query in a thousand other places, and it works everywhere else.  For example:

 

$sql = "SELECT * FROM auctions WHERE type='Boats' AND state='$v4' ORDER BY $v1 $v2";

 

works just fine.  I'm not exactly an expert on any of this, but I can see no logical reason why this works, but the Search code doesn't.  They appear in all ways identical, at least as far as query structure goes.  Can anyone spot where I screwed up?

Thanks!

Kyle

 

Link to comment
Share on other sites

You'll need to force the precedence with parentheses.

 

Ah.  Is that because of the recursive searchterm instances?  I'm trying to pound the concepts into my head-  is it conceptually the same thing as writing "WHERE type='y' AND name LIKE 'z' OR type='y' AND name LIKE 'z'  OR type='y' AND name LIKE 'z'"?

In other words, the type has to be set for each Or statement?  I'm trying to wrap my head around the concepts so that one day I shall return, a .php Jedi Master!  ;)

Thanks Pika!

Link to comment
Share on other sites

$result = mysql_query("SELECT * FROM auctions WHERE type='Cars' AND (  name LIKE '%".$searchterm."%' OR Address LIKE '%".$searchterm."%' OR state like '%".$searchterm."%' )");

 

 

MySQL row:
type    - Cars
name    - testing
address - 123 fake street
state   - enabled


SEARCHTERM - 'fake street'


WHERE CLAUSE:
WHERE true AND false OR true OR false  // this will result true if any argument equates to true
WHERE true AND ( false OR true OR false ) // this will return true if argument 1 AND ( 2 OR 3 OR 4 ) are true

Link to comment
Share on other sites

The way it is now, it will return results that match WHERE type='Cars' AND  name LIKE '%$searchterm%, but it will also return results that match OR Address LIKE '%".$searchterm."%' OR state like '%$searchterm%'" regardless of whether they match the first two parameters.

Link to comment
Share on other sites

I rewrote it like so:

$result = mysql_query("
	SELECT * FROM auctions 
	WHERE type='Cars' 
	AND (name LIKE '%".$searchterm."%' 
			OR Address LIKE '%".$searchterm."%' 
			OR city LIKE '%".$searchterm."%' 
			OR phone LIKE '%".$searchterm."%' 
			OR zip LIKE '%".$searchterm."%' 
			OR website LIKE '%".$searchterm."%' 
			OR state like '%".$searchterm."%')
	");

 

But of course, this being me, it's still doing the exact same thing as before: returning ALL types.  Clearly there's something I'm not grasping: doesn't the "WHERE type='Cars', outside of the Parens, force the query to assume Type as a per-requisit to the results within the parens?

Thanks!

Link to comment
Share on other sites

Should work :s

 

It's not the first time that my code has defied the laws of the universe.  :)

 

EDIT: As it turns out, it was that laziest of errors on my part, "not refreshing the cache".  I shall now hang my head in shame and contemplate seppeku.

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.