hi, how could i go about searching a mysql database for keywords.
EG: I have a table called 'stories' with fields "id,title,description,text".
what would be the SQL statement, using keywords froma form($keywords).
many thanx
searches
Moderator: General Moderators
-
JPlush76
- Forum Regular
- Posts: 819
- Joined: Thu Aug 01, 2002 5:42 pm
- Location: Los Angeles, CA
- Contact:
Code: Select all
<?php
// START CHECK 1 FOR BLANK SEARCH TERM
if($_SESSION['search_term'] != '' AND strlen($_SESSION['search_term']) > 2)
{
$formdata = $_SESSION['search_term'];
$text = '';
$the_array = explode(' ', $formdata);
$i = 0;
foreach($the_array AS $t)
{
if( $i == 0 )
{
$text .= " (products_name LIKE '%$t%' OR products_description LIKE '%$t%' OR products_model LIKE '%$t%') ";
$i = 1;
}else{
$text .= " AND (products_name LIKE '%$t%' OR products_description LIKE '%$t%' OR products_model LIKE '%$t%') ";
}
}
?>Code: Select all
<?php
Select * FROM table WHERE $text
?>-
JPlush76
- Forum Regular
- Posts: 819
- Joined: Thu Aug 01, 2002 5:42 pm
- Location: Los Angeles, CA
- Contact:
also...
I used to use mysql full text search but its kinda limited for a shopping site.
It only searches for words bigger than 3 characters. Well one of the popular cameras we sell is the Nikon N80. If someone searches for N80 nothing will come back. Also, if Someone searched for Kodak Film full text would return everything that contained Kodak or Film.
Until the full text gets better its probably better to stick with using "LIKE"
unless you have hundreds of thousands of rows.
I used to use mysql full text search but its kinda limited for a shopping site.
It only searches for words bigger than 3 characters. Well one of the popular cameras we sell is the Nikon N80. If someone searches for N80 nothing will come back. Also, if Someone searched for Kodak Film full text would return everything that contained Kodak or Film.
Until the full text gets better its probably better to stick with using "LIKE"
unless you have hundreds of thousands of rows.