php how can I make my search engine search for mutiple keywords?
this is what I have so far:
<?php
include "config.php";
if (@$_POST['search']=="search")
{
$keyword=$_POST['keyword'];
$result=mysql_query("SELECT * FROM listed WHERE `itemname` LIKE ‘%$keyword%’ OR `message1` LIKE ‘%$keyword%’");
while ($results = mysql_fetch_array($result))
{
$tag=$results['tag'];
$idno=$results['id'];
$title=$results['name'];
$description=$results['message1'];
$position=40;
$post = substr($description, 0, $position);
echo "<b>$tag,$title,$post,$idno</b>
?>
I also heard of adding a thing called noslashes or something which is supposed to prevent unwanted SQL queries. Any info on that?
2 Responses
dt01pqt_pt
20 Feb 2010
ladeehwk
20 Feb 2010
You didn’t say which search engine you use, I use Google and they have an advanced search area that lets you input the words that you want or you can search on a word or phrase and then go to the bottom of the page and click on ‘search within these results’ to narrow things down.
Hope this helps





Try this:
<?php
include "config.php";
if (@$_POST['search']=="search")
{
@$keywords=split(" ",$_POST['keyword']);
foreach ($keywords As $keyword){
$keyword = get_magic_quotes_gpc() ? addslashes(trim($keyword)) : trim($keyword);
$search[]="itemname LIKE ‘%$keyword%’ OR message1 LIKE ‘%$keyword%’";
}
$search = join("OR ", $search);
$result=mysql_query("SELECT * FROM listed WHERE($search)");
while ($results = mysql_fetch_array($result))
{
$tag=$results['tag'];
$idno=$results['id'];
$title=$results['name'];
$description = $results['message1'];
$position=40;
$post = substr($description, 0, $position);
echo "<b>$tag,$title,$post,$idno" . "</b><br>";
}
}
?>
I had to guess a few things because Yahoo put ellipse (…) on some of the words in your post. So can change it if it is wrong