|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here! |
|
#1
|
|||
|
|||
|
I'm working on a search engine using MySql database and PHP, and would like to allow users not to fill in all fields. I would like to right a generic query that in case a field was not filled in, a key word such as "any" or so would be inserted and would return all entries in the table..Is there such a key word in MySql?
------------------ michael stange |
|
#2
|
|||
|
|||
|
If you can't leave it null, just give a default for the field when you define the table.
|
|
#3
|
|||
|
|||
|
Thanks Rod, but thats not exactly what I ment. The user of the search engine might leave a few of the search fields empty, and when I pose the query I would like for those fields to return all entries in the matching table. For example if he wishes to search for a book and only enters the Autors name I want the my one query to fetch all the books that were writen by that Author and because he has the option to also add to the search a year of publication I want, in case it's null to put in a general key word like "any" that will just return all books from any year... Sorry if I wasn't clear with my question.
------------------ michael stange |
|
#4
|
|||
|
|||
|
Ahh, I see.
Well, you could do one of two things. First, if you build your query properly it shouldn't matter if the field is blank: $query="select * from table_name where author LIKE '%$author%' AND publisher LIKE '%$pub%'"; You see if the field was empty on the form it would send to mysql field LIKE '%%' which should match everything. Another option is to build the query dynamically (assuming text fields are in an array): $query="select * from table where"; $add=0; while (list($key,$value)=each($search_term)) { if ($value) { if (!$add) $query.=" AND"; $add++; $query.="$key LIKE '%$value%'"; } } If you make the array index the same as the column in the table it makes it easier as you can see. HTH |
|
#5
|
|||
|
|||
|
K Thanks, I'll give it a try.
It sounds like a very good solution... |
![]() |
| Viewing: Dev Shed Forums > Databases > MySQL Help > Looking for a global key word... |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|