Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 08-10-2004, 03:02 PM Searching Substring
Novice Talker

Posts: 7
Trades: 0
How do I search substrings using PHP for items in a MySQL table? Right now my code only returns results if the entire string is inputted in the search field.

Thansk
__________________

Please login or register to view this content. Registration is FREE
hddweb is offline
Reply With Quote
View Public Profile Visit hddweb's homepage!
 
 
Register now for full access!
Old 08-10-2004, 04:26 PM
Kyrnt's Avatar
The Post-Mod Years

Posts: 2,536
Location: Western Maryland
Trades: 0
It depends on where you want to do the search. If you want to check for a substring in PHP, use the substr() function.

If you want to perform the search in SQL, there are a variety of SQL functions to be used. Go here to see all the functions for string searching the MySQL supports. Right off hand I see INSTR() and LOCATE() would probably do the job you want.
__________________
—Kyrnt
Kyrnt is offline
Reply With Quote
View Public Profile Visit Kyrnt's homepage!
 
Old 08-10-2004, 07:49 PM
webwoRRks's Avatar
Ultra Talker

Posts: 426
Location: I hope so
Trades: 0
or, if your searching for keywords in the text field of a mysql table, use this;

SELECT blah FROM blah_table WHERE keywords LIKE "%search_word%"
__________________
Theres 10 types of people; those who understand binary, and those who don't.
webmaster and webdeveloper resources,
Please login or register to view this content. Registration is FREE
webwoRRks is offline
Reply With Quote
View Public Profile Visit webwoRRks's homepage!
 
Old 08-11-2004, 02:32 AM
Novice Talker

Posts: 7
Trades: 0
So basically this is a search page. I have a bunch of products in a MySQL table and I need the script to search names of the products in the table and display it on the page. However since the product names have like dashes and () and stuff like that (biochem stuff :/) inputting an exact search of the product name will be very time consuming for the user/hard to get accurate. Thus I need the search script to be able to take in the users search term and look through the mysql table for any entry with the search term included anywhere in the name.
__________________

Please login or register to view this content. Registration is FREE
hddweb is offline
Reply With Quote
View Public Profile Visit hddweb's homepage!
 
Old 08-11-2004, 04:40 AM
Ultra Talker

Posts: 377
Trades: 0
you may read a value from a table and parse it... there are a lot of functions in php to do this - you may use standard, or a function for work with regular expressions... all is as Krynt said
__________________
andrews_john

Please login or register to view this content. Registration is FREE
andrews_john is offline
Reply With Quote
View Public Profile Visit andrews_john's homepage!
 
Old 08-11-2004, 07:03 AM
webwoRRks's Avatar
Ultra Talker

Posts: 426
Location: I hope so
Trades: 0
My directory script does this;

Take in input in a text field, allows - (minus) to show you don't want that term included;

PHP Code:
<?
$words 
explode(" "strtolower($_GET['query']));

$sql "";

foreach(
$words as $key=>$value) {
  if(
$value{0}=='-') { $sql .= " AND product_title NOT LIKE '%".substr($value,1,strlen($value))."%'"; }
  else { 
$sql .= " AND product_title LIKE '%$value%'"; }
}
?>
so you can tag $sql onto your query to search. Thus, a search for "tube" would match

"test-tubes"
fake test-tubes"

but a search for "tube -fake" would only match

"test-tubes"

That help at all?
__________________
Theres 10 types of people; those who understand binary, and those who don't.
webmaster and webdeveloper resources,
Please login or register to view this content. Registration is FREE

Last edited by webwoRRks; 08-11-2004 at 07:04 AM.. Reason: typo
webwoRRks is offline
Reply With Quote
View Public Profile Visit webwoRRks's homepage!
 
Old 08-11-2004, 09:12 AM
Republikin's Avatar
Defies a Status

Posts: 3,189
Trades: 3
An even cleaner and more effiecent search method if you have mysql 4.1+ is full text searching.

http://dev.mysql.com/doc/mysql/en/Fulltext_Search.html
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
Republikin is offline
Reply With Quote
View Public Profile
 
Old 08-11-2004, 03:15 PM
Novice Talker

Posts: 7
Trades: 0
Thanks for the help everyone. I'll try the different methods and see what works best.
__________________

Please login or register to view this content. Registration is FREE
hddweb is offline
Reply With Quote
View Public Profile Visit hddweb's homepage!
 
Reply     « Reply to Searching Substring
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.27154 seconds with 12 queries