Google Answers Logo
View Question
 
Q: Need PHP code to convert a boolean search string into SQL ( Answered 4 out of 5 stars,   0 Comments )
Question  
Subject: Need PHP code to convert a boolean search string into SQL
Category: Computers > Programming
Asked by: trancecan-ga
List Price: $40.00
Posted: 02 Jun 2006 13:44 PDT
Expires: 02 Jul 2006 13:44 PDT
Question ID: 734832
Well, after banging my head against the wall for a while, and thinking
this will require a regular expression or a few (which are mindbending
to me) I thought I'd see if someone here might be able to whip
something up for this.

We have a search form and we would like to allow people to enter
boolean terms to help them narrow their search.  So, for example the
code I'm looking for would take something like this:

$_POST[thefieldtosearch]
  would have "Running OR Walking AND Tall AND Happy NOT short"

The code needs to watch for " OR ", " AND ", " NOT " and my example
here should end up as SQL such as this:

$sql = "SELECT * from mydb where thefieldtosearch LIKE '%Running%' OR
thefieldtosearch LIKE '%Walking%' AND thefieldtosearch LIKE '%Tall%'
AND thefieldtosearch LIKE '%Happy%' NOT thefieldtosearch LIKE
'%short%'";

I'm sure that's not exactly correct syntax, and the query would likely
need to be built piece by piece as the original string is being broken
down..but that's an example of the final query as seen by the db.

Clarification of Question by trancecan-ga on 02 Jun 2006 13:55 PDT
P.S.  The answer should also be "SQL Injection" SAFE

THANKS!
Answer  
Subject: Re: Need PHP code to convert a boolean search string into SQL
Answered By: tox-ga on 04 Jun 2006 03:28 PDT
Rated:4 out of 5 stars
 
trancecan-ga,

I have written an include file so you can easily add this
functionality to your code:

www.maxlin.ca/boolean-query.zip

To use, simply copy the file into the directory of the script you want
to use it, and add at the beginning of your file:

<? include ("boolean-query.php"); ?>

To use the function, call:

boolstring2sql (<return fields>, <search tables>, <search fields>, <search string>)

For example:
echo boolstring2sql ("*","mydb","thefieldtosearch", "Running OR
Walking AND Tall AND Happy NOT short");

Returns:
SELECT * FROM mydb WHERE (thefieldtosearch LIKE '%running%') OR
(thefieldtosearch LIKE '%walking%') AND (thefieldtosearch LIKE
'%tall%') AND (thefieldtosearch LIKE '%happy%') AND (NOT
thefieldtosearch LIKE '%short%');

If you only want the WHERE portion, use:

boolstring2sql_query (<search fields>, <search string>)

For example:

boolstring2sql_query ("thefieldtosearch", "Running OR Walking AND Tall
AND Happy NOT short")

Returns:

(thefieldtosearch LIKE '%running%') OR (thefieldtosearch LIKE
'%walking%') AND (thefieldtosearch LIKE '%tall%') AND
(thefieldtosearch LIKE '%happy%') AND (NOT thefieldtosearch LIKE
'%short%')

To specify multiple fields, use space delimiting. For example:

boolstring2sql ("*","mydb","firstfield secondfield thirdfield",
"Running OR Walking AND Tall AND Happy NOT short");

Returns:

SELECT * FROM mydb WHERE (firstfield LIKE '%running%' OR secondfield
LIKE '%running%' OR thirdfield LIKE '%running%') OR (firstfield LIKE
'%walking%' OR secondfield LIKE '%walking%' OR thirdfield LIKE
'%walking%') AND (firstfield LIKE '%tall%' OR secondfield LIKE
'%tall%' OR thirdfield LIKE '%tall%') AND (firstfield LIKE '%happy%'
OR secondfield LIKE '%happy%' OR thirdfield LIKE '%happy%') AND (NOT
firstfield LIKE '%short%' OR secondfield LIKE '%short%' OR thirdfield
LIKE '%short%');

It should be noted that the search string is guarded against SQL injection.


Enjoy,

tox-ga

Request for Answer Clarification by trancecan-ga on 05 Jun 2006 11:38 PDT
Hi again!

We stumbled into a "bug", maybe not so much a bug as something I might
not have clarified.  When doing a search such as:

training AND (cat OR dog)

We end up with this sql:

...WHERE (objective LIKE '%training%') AND (objective LIKE '%(cat%')
OR (objective LIKE '%dog)%')

Which is ALMOST correct, but it's not stripping out the original () in
the query.  '%(cat%')  should be '%cat%'

I am not SQL wiz, so I'm not sure what the exact correct sql is for
that, I've been running some queries and both of these seem to give me
the same results:

..WHERE (objective LIKE '%training%') AND ((objective LIKE '%cat%') OR
(objective LIKE '%dog%')) ...
..WHERE (objective LIKE '%training%') AND (objective LIKE '%cat%') OR
(objective LIKE '%dog%') ...

That could just be a fluke because of precedence.  Is suspect you will
know which way is truly correct.  Can you make this change?  If it's a
huge issue to rewrite for the () around the whole OR, if we can just
get the ( and ) removed from the actual query terms that would be
great.  It is safe to assume that any ( ) in the search terms is meant
to dictate the precedence.  There is no need for anyone to actually be
searching for a ( or ) in their terms.

Is this quickly/easily do-able?  Am I asking too much of you?
trancecan-ga rated this answer:4 out of 5 stars
Very good answer, I had to make a minor tweak to get it working for
"NOT" queries.  Line 41 of boolean-query.php I had to change $front =
"(NOT ";  to $front = "NOT (";
Otherwise seems to work great!!  THANKS!!

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy