[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2009-05-20
10
Medium Priority
?
556 Views
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.
0
Comment
Question by:x13
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
10 Comments
 
LVL 6

Expert Comment

by:birwin
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.
0
 
LVL 6

Expert Comment

by:birwin
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
0
 

Author Comment

by:x13
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?
0
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
LVL 6

Expert Comment

by:birwin
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?
0
 

Author Comment

by:x13
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?
0
 

Author Comment

by:x13
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.
0
 
LVL 6

Accepted Solution

by:
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.
0
 

Author Closing Comment

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

Featured Post

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

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

Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

656 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