Page 1 of 1

searches

Posted: Fri Dec 13, 2002 9:35 am
by blueprint
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

Posted: Fri Dec 13, 2002 10:10 am
by Johnm
To write the querry you need to know a few more things...
Do you want to retrieve everything from the table?
Do you want things ordered and if so how?
Think through what you need and post it.

John M

Posted: Fri Dec 13, 2002 10:16 am
by blueprint
what i need is this:

theres a form where the user can put keywords into a textbox.
find all occurences of the keywords,
then links to the actual story are created using the results of the search(the links would be called the $title of the 'story') in a simple list.

hope that makes it clearer.

Posted: Fri Dec 13, 2002 10:39 am
by JPlush76

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


?>
thats the way I do it

Posted: Fri Dec 13, 2002 10:40 am
by Johnm
Can't argue that.


John M

Posted: Fri Dec 13, 2002 10:43 am
by JPlush76
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.

Posted: Sat Dec 14, 2002 3:57 pm
by blueprint
okeydoke, thanx alot =].