Hi,
With the help of some code from the forum and a little adjustment here and there I created a search script that supports multiple words from a database. That was the hard part, now the easy part of querying the database is where it all goes downhill 🙁
here is the code:
<?
// my search string was 'please read as this contains'
$searchwords = $HTTP_POST_VARS['search'];
$db = mysql_connect("localhost","username","password");
mysql_select_db("database",$db);
$words = split(" +",addslashes($searchwords));
$query = "SELECT * FROM search WHERE ";
$once=0;
$number = count($words);
$try = $number - 1;
foreach($words as $word)
{
$query.= " info LIKE '%$word%'";
if ($once < $try)
$query.= " OR";
if ($once == $try)
$query.= "";
$once++;
}
echo $query;
// the query looks something like SELECT * FROM search WHERE info LIKE '%please%' OR info LIKE '%read%' OR info LIKE '%as%' OR info LIKE '%this%' OR info LIKE '%contains%'
$sql = mysql_query($query,$db);
$array = mysql_fetch_array($sql);
$result = $array['output'];
echo $result;
?>
I keep getting an error that says:
Warning: Supplied argument is not a valid MySQL result resource
Anyone help?
Thanks
Ant