How to build an SQL query for multiple form fields..?

I have a mySQL database that has about 65 or so fields, each related to an input field on an HTML form. Many of the fields are checkboxes (arrays) on the HTML field, which I have "imploded" into comma separated strings for the database. I have been using "strpos" in order to check if a value exists and display the desired results. (Overcoming the checkbox problem was a big obstacle for me....)

I would like to be able to search my mySQL database,  on a variety of different criteria. So my search form would have the same fields (almost) as my input form, and I could search on text value(s) and on checkbox value(s).

My problem is how to build an SQL statement that searches multiple fields for multiple values. All the examples I've come across show only simple SQL examples. If anyone can give any suggestions or links to a good tutorial that would be great.
x13Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
birwinConnect With a Mentor Commented:
Hi x13:
Searching your packed fields is difficult if you are also trying to search within unpacked fields without breaking them out using a temporary table.
If you are searching only within packed fields, and you want an exact search, you can pack your search term with the separator, however a comma is not the best separator to use, since it can be included in regular text. A bar | (the character over \ on most keyboards) is a better separator. You can then take your search term, say "blue" and pack it with the bars. $search_term="|".$serach_term."|";  
Now if your field is |blue|green|yellow|orange and purple|blue and green|blue, yellow and fuchsia|   you are only searching for |blue| so only the first instance will be found.
0
 
birwinCommented:
You need to use a full text search. I had this same issue.
Here is the code I finaaly used. Obviously your table names and fields will be different:
"SELECT id, title,  summary,  content, tags, thumb, subcategory_id, author_id,  MATCH (title,  summary,  content, tags ) AGAINST ('$search_term' IN BOOLEAN MODE) AS score FROM mydatabasename WHERE published=1 AND  MATCH (title,  summary,  content, tags )  AGAINST  ('$search_term' IN BOOLEAN MODE) LIMIT $start, 12 ");
Note that you need to do a full text index on your database. The "dual" match increases the relevance scoring. The $start and l2 are used for pagination.
0
 
birwinCommented:
Full text search only works with the MyISAM storage engine.
Create the FULL TEXT index with:
ALTER TABLE tablename ADD FULLTEXT(title,  summary,  content, tags);
Also my use of mydatabasename in the MATCH AGAINST statement is misleading. It should read tablename
0
Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

 
x13Author Commented:
How do I know which storage engine I have? My MySQL version is quite old (4.1).

So if I want to search against let's say 8 fields (and my table were full text indexed), I could say something like:

SELECT (field1, field2, field3, ... field8) MATCH (field1, field2) AGAINST ("inputted text" IN BOOLEAN MODE) AS score FROM tablename AND MATCH (field3) AGAINST ("checkbox value") AND MATCH (field4) AGAINST ("checkbox value) AND MATCH (field5) AGAINST ("checkbox value") AND ... ?

Also what does "score" represent? Is it a field in the table? I assume it's a relevance score?
0
 
birwinCommented:
If you are using the standard mySQL setup, you are using the MyISAM storage engine.
The original select does not have to be identical to the MATCH fields, but the two MATCH fields should be the same.
The "score" is an alias, it is not an actual field. It is used to allow the results to be priortized by relevance.
 To correct your statement:
SELECT (field1, field2, field3, ... field8) MATCH (field1, field2) AGAINST ("inputted text" IN BOOLEAN MODE) AS score FROM tablename AND MATCH (field1, field2) AGAINST ("inputted text" IN BOOLEAN MODE") AND MATCH (ffield1, field2) AGAINST ("inputted text" IN BOOLEAN MODE)  
Now searching your checkbox fields is another matter. Are these packed fields alpha or numeric? Can you show me a sample of the data from one of these fields? (e.g. is it "blue, green, yellow, red" or "1,3,5,2" and how many options are their in an entry?
0
 
x13Author Commented:
So, if I understood correctly, you can only search a limited number of fields in one query. :(

So for example, if my table contains a field "colour" with checkbox values "red", "green", "yellow", "blue", "purple" and another field "units" with checkbox values "miles", "feet", "inches", "metres", "cups", "degrees"

Now say I want all records containing the checkboxes "red" and "blue" and also "inches" "cups" and "degrees" (and query - not OR)

SELECT (colour, units) MATCH (colour) AGAINST ("red, blue" IN BOOLEAN MODE) AS score FROM tablename AND MATCH (units) AGAINST ("inches cups degrees" IN BOOLEAN MODE);

From what I undestand above this cannot be done? Unless I search all fields repeatedly against each string? Like

SELECT (colour, units) MATCH (colour, units) AGAINST ("red blue" IN BOOLEAN MODE) AS score FROM tablename AND MATCH (colour, units) AGAINST ("inches cups degrees" IN BOOLEAN MODE);

?? Which would work?
0
 
x13Author Commented:
Oh I forgot to say that the checkbox values are entered into an array in php, then imploded into a comma separated string. So in the MySQL database, a checkbox field ends up formatted as a single string that looks like "red, green, blue, purple". I use php function explode() to make it back into an array for display purposes.
0
 
x13Author Commented:
Thank you for your help and making the answer clear! and sorry for the delay in getting back.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.