Jump to content

issues with search using SELECT * FROM table WHERE field LIKE 'words'


jeger003

Recommended Posts

i want to create a simple search that would just search for a few words. My problem is when i enter more than one word it would search for the exact phrase. I need it to search for any matching words.. dont have to be in order.

 

i would appreciate some help.

<?php
//from the searchform
$search_words = "homer simpson peter griffin";
$search_words = explode($search_words, " ");
foreach($search_words as $search_word){
mysql_query("SELECT * FROM table WHERE field LIKE $search_word")
?>

 

I dont think that solves the problem completely but should get you moving in the right direction

<?php
//from the searchform
$search_words = "homer simpson peter griffin";
$search_words = explode($search_words, " ");
foreach($search_words as $search_word){
mysql_query("SELECT * FROM table WHERE field LIKE $search_word")
?>

 

thanks for the help lodius2000

 

i used your code and played around with it but i am not able to get it to work. I either get an error saying the foreach() is not used correctly or i would get a blank screen

 

this code below i get a blank screen

<?php

<?php

include "db.php";

ini_set('error_reporting', E_ALL);

//from the searchform
$search_words = "used car for sale";
$search_words = explode($search_words, " ");
foreach($search_words as $search_word)
{
$query = mysql_query("SELECT title, description, search_text FROM listings WHERE search_text LIKE '%$search_word%' OR title LIKE '%$search_word%'") or die(mysql_error());


while($result = mysql_fetch_array($query))
{
echo $result;	
}
}



?>

 

 

use a stop word filter to filiter words that should be ignored, that way you return only relevant results... Then after you fliter your words... use a regex to only return whole word matches...

 

$search_words = "used car for sale";

$search_words = implode ( '|', array_map ( 'trim', explode ( ' ', $search_words ) ) );

$query = mysql_query("SELECT title, description, search_text FROM listings WHERE search_text REGEXP '[[:<:]]". $search_words . "[[:>:]]';") or die(mysql_error());

 

use a stop word filter to filiter words that should be ignored, that way you return only relevant results... Then after you fliter your words... use a regex to only return whole word matches...

 

$search_words = "used car for sale";

$search_words = implode ( '|', array_map ( 'trim', explode ( ' ', $search_words ) ) );

$query = mysql_query("SELECT title, description, search_text FROM listings WHERE search_text REGEXP '[[:<:]]". $search_words . "[[:>:]]';") or die(mysql_error());

 

 

for some reason i continue to get a a blank screenn i even have error_reporting on

 

ini_set('error_reporting', E_ALL);

$search_words = "used car for sale";

$search_words = implode ( '|', array_map ( 'trim', explode ( ' ', $search_words ) ) );

$query = mysql_query("SELECT title, description, search_text FROM listings WHERE search_text REGEXP '[[:<:]]". $search_words . "[[:>:]]';") or die(mysql_error());


while($row = mysql_fetch_array($query))
{
echo $row['title'];
}

 

 

i removed everything having to do with REGEXP and it brought back results so i know its working

use a stop word filter to filiter words that should be ignored, that way you return only relevant results... Then after you fliter your words... use a regex to only return whole word matches...

 

$search_words = "used car for sale";

$search_words = implode ( '|', array_map ( 'trim', explode ( ' ', $search_words ) ) );

$query = mysql_query("SELECT title, description, search_text FROM listings WHERE search_text REGEXP '[[:<:]]". $search_words . "[[:>:]]';") or die(mysql_error());

 

WHOA! it works! i was made it search in titles instead of search_text. But one last question now. It possible it make it search in titles, description, and search_text??

 

could i just use OR like this??

 

WHERE search_text REGEXP '[[:<:]]". $search_words . "[[:>:]]' OR title REGEXP '[[:<:]]". $search_words . "[[:>:]]' OR description REGEXP '[[:<:]]". $search_words . "[[:>:]]'"

 

 

or is there another way to do it?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.