SunQuest
           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 June 29th, 2000, 05:49 PM
RyanP
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
I have an input box where users can input word(s) to search through the database and I'm using Perl DBI and placeholders for the WHERE LIKEs for each field, but how would you get this to work for multiple word searches?

For example, as I have it set up right now, if a user searched for "John Doe" (and a record exists that has "John" in the firstName field and "Doe" in the lastName field, 0 matches will be returned because not one of the fields contains the entire string of "John Doe".

Obviously, this is not good. Is there any way around this?

Thanks,
RyanP

[This message has been edited by RyanP (edited June 29, 2000).]

Reply With Quote
  #2  
Old June 29th, 2000, 07:23 PM
Jinn Jinn is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Posts: 13 Jinn User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Why don't you just put two search field: firstName & lastName?


------------------
---------
Stories OnLine
http://www.stories-online.net/

WARNING: adult stories!

Reply With Quote
  #3  
Old June 29th, 2000, 10:12 PM
RyanP
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
There are several other fields in the table too and I have included an optional pull down menu to select a particular field to search in, but I also want to provide the option to search in all fields with multiple word searches.

Reply With Quote
  #4  
Old June 30th, 2000, 06:56 AM
Kyuzo Kyuzo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 1999
Location: Annapolis, Maryland US
Posts: 113 Kyuzo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
if the $searchstring is "John Doe"....

# split $searchstring between occurrence(s) of spaces

@search_list=split(/ +/, $searchstring);

$sql="select * from table_name where ";

for($i=0; $i<@search_list; $i++)
{
$sql.="field_name like '%$search_list[$i]%' OR ";
}

# now remove trailing "OR "

$sql=substr($sql, 0, -3);

execute the query....

[This message has been edited by Kyuzo (edited June 30, 2000).]

Reply With Quote
  #5  
Old June 30th, 2000, 11:27 AM
RyanP
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Thanks Kyuzo, that is just what I was looking for. Although I would prefer to use placeholders and bind_params, this is probably the only way to do this. Because I will be inserting variables directly into the query statement, what characters do I need to filter out of the search string? Just single quotes with ' ?

[This message has been edited by RyanP (edited June 30, 2000).]

Reply With Quote
  #6  
Old June 30th, 2000, 01:39 PM
lhallusa lhallusa is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Posts: 43 lhallusa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
Send a message via ICQ to lhallusa
Actually there is one other better way to do this using regular expressions.
$string = "John Doe";
$string =~ s/ /|/g;
this replaces spaces with the | character. You end up with John|Doe
Then the query is:
$sql = "SELECT * FROM table_name WHERE lcase(field_name) REGEXP lcase($string)";
The query can be extended to search multiple fields by using OR AND etc.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > How to perform a variable word count search?


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 1 hosted by Hostway