RE: [PHP] Help needed - search problem

From: Date: Thu, 15 Mar 2001 19:48:42 +0000
Subject: RE: [PHP] Help needed - search problem
Groups: php.general 
Request: Send a blank email to php-general+get-44105@lists.php.net to get a copy of this message
> If the keywords inputted from user are: "word2, word1", it > will reture News1 twice: That's because you are selecting all the records that match each single key word individually. > I want to search this table based on the Keywords. the code is: > $split = explode(" ",$keywords); > $Num = 0; > for($index=0;$index<count($split);$index++){ > $sql = "SELECT * FROM News WHERE Keywords LIKE '%$split[$index]%'; > $result = mysql_query($sql) or die(mysql_error()); > while($myrow = mysql_fetch_array($result)){ > $Num = $Num +1; > $NewsID = $myrow["NewsID"]; > $News = $myrow["News"]; > echo $Num . ". " . $News . "<br>"; > }//end while > }//end for What you want to do is something like this: $keyWordsArray = explode( " ", $keywords ); $keyWordsString = implode( "%\" OR \"%", $keyWordsArray ); $sql = "SELECT * FROM News WHERE Keywords LIKE \"$keyWordsString\""; to make one query that will select all the records with matching keywords. Depending on what you are exploding on, you could use str_replace() instead of explode() then implode(). Chris

« previous php.general (#44105) next »