Jump to content

Mysql Search function


Gazan

Recommended Posts

Hey all.

 

I'm stuck doing a mysql search function for my project.. What i have now, only returns results that matches 1 keyword in my form. What i want to, is to search for multiple keywords. I've been googling around, reading some tutorials, but i don't get the concept of explode() and storing the keywords into and array, and then do a query where you search for keywords that matches that array.. I need some explanation on how to do this.

 

Current code:

 

if(isset($_POST['keyword'])) {

$keyword = $_POST['keyword'];

if(empty($keyword)) {
	echo "You need to type a keyword to search for.";
}

else {

	$keyword = mysql_real_escape_string($keyword);
	$sql = "SELECT * FROM nyheder WHERE news_subject OR news_content LIKE '%".$keyword."%' ORDER BY news_date AND news_time";
	$result_search_news = mysql_query($sql);
	$count_search_news = mysql_num_rows($result_search_news);

	if($count_search_news==0) {
		echo "You search for "<b>".$keyword."</b>" gave no results.";
	}
	else {

		echo "Your search for "<b>".$keyword."</b>" gave ".$count_search_news." results:";
		echo "<br /><br />";

		while($news_search = mysql_fetch_array($result_search_news)) {
			echo $news_search['news_subject'];
			echo "<br />";
		}
	}
}

}

Link to comment
https://forums.phpfreaks.com/topic/144483-mysql-search-function/
Share on other sites

Personaly assuming you don't need any advanced query options then just do a preg_replace('/ /', '%', $searchPhrase);

This will replace all the spaces with the MySQL wildcard character, this is not the best way (or even nesicarily a good one) but it works well enough for phrases.

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.