MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old July 21st, 2000, 10:35 PM
mikey mikey is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2000
Location: Melbourne Australia
Posts: 3 mikey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
  #2  
Old July 22nd, 2000, 06:33 AM
rod k rod k is offline
Apprentice Deity
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Jul 1999
Location: Niagara Falls (On the wrong side of the gorge)
Posts: 3,237 rod k User rank is Private First Class (20 - 50 Reputation Level)rod k User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 m 8 sec
Reputation Power: 13
Send a message via AIM to rod k
If you can't leave it null, just give a default for the field when you define the table.

Reply With Quote
  #3  
Old July 22nd, 2000, 06:46 AM
mikey mikey is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2000
Location: Melbourne Australia
Posts: 3 mikey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
  #4  
Old July 22nd, 2000, 02:34 PM
rod k rod k is offline
Apprentice Deity
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Jul 1999
Location: Niagara Falls (On the wrong side of the gorge)
Posts: 3,237 rod k User rank is Private First Class (20 - 50 Reputation Level)rod k User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 m 8 sec
Reputation Power: 13
Send a message via AIM to rod k
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

Reply With Quote
  #5  
Old July 22nd, 2000, 10:23 PM
mikey mikey is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2000
Location: Melbourne Australia
Posts: 3 mikey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
K Thanks, I'll give it a try.
It sounds like a very good solution...

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > Looking for a global key word...


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway