Hello-
I've written a simple keyword search program using PHP and MySql. The keyword comes from a simple search form. But it only works if the user enters one keyword. If they enter more than one search term, the search returns no results. How can this be corrected? I modified some code I found, but it doesn't work for multiple terms either (the code does work for single term). The code I wrote appears like it should work, but if someone could tell me why it doesn't I'd appreciate it. It works for single search terms but if multiple search terms are entered, it fails with a sql error. I think the problem is its appending an extra OR on the end of the query string, but I can't figure out how to stop it. Or is their a better way to solve the multiple keyword problem? Thanks for any help.
<?php
//title is the columname
$keywords = $HTTP_POST_VARS['keywords'];
if ($searchjob == "SEARCH"){
$pieces = explode (" ", $keywords);
$count=count($pieces);
$query="SELECT * FROM db_table WHERE ";
if ($count==1){
$query.="title LIKE '%$keywords%'";
}elseif ($count>=1){
for($a=0;$a<$count;$a++){
$query.="title LIKE '%$pieces[$a]%'";
if ($a != $count){
$query.=" OR ";
}
}
}
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
echo (" " . $row["title"] . " ");
}
}
?>