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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.