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

Posted on 2009-05-20
Medium Priority
Last Modified: 2013-12-12
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.
Question by:x13
  • 4
  • 4

Expert Comment

ID: 24436470
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.

Expert Comment

ID: 24436677
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

Author Comment

ID: 24466615
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?
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.


Expert Comment

ID: 24495622
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?

Author Comment

ID: 24581430
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?

Author Comment

ID: 24581451
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.

Accepted Solution

birwin earned 1600 total points
ID: 24654822
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.

Author Closing Comment

ID: 31583636
Thank you for your help and making the answer clear! and sorry for the delay in getting back.

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
How much do you know about the future of data centers? If you're like 50% of organizations, then it's probably not enough. Read on to get up to speed on this emerging field.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question