searches

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
blueprint
Forum Newbie
Posts: 5
Joined: Fri Dec 13, 2002 8:50 am

searches

Post 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
User avatar
Johnm
Forum Contributor
Posts: 344
Joined: Mon May 13, 2002 12:05 pm
Location: Michigan, USA
Contact:

Post 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
blueprint
Forum Newbie
Posts: 5
Joined: Fri Dec 13, 2002 8:50 am

Post 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.
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post 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
User avatar
Johnm
Forum Contributor
Posts: 344
Joined: Mon May 13, 2002 12:05 pm
Location: Michigan, USA
Contact:

Post by Johnm »

Can't argue that.


John M
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post 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.
blueprint
Forum Newbie
Posts: 5
Joined: Fri Dec 13, 2002 8:50 am

Post by blueprint »

okeydoke, thanx alot =].
Post Reply