Solved

Search mysql db with multiple keywords

Posted on 2004-09-06
4
1,051 Views
Last Modified: 2013-12-12
I am trying to figure out how to search 3 columns in a mysql table with multiple keywords.

So if I get - Blue book apple – from a text field I want to search my db and return records that contain these keywords.  I want the users to be able to enter the search criteria from a text field; this means I don’t know the number of search terms.  

I am really stuck on this one?
0
Comment
Question by:bliesveld
4 Comments
 
LVL 5

Expert Comment

by:webtrans
Comment Utility
can u explain this more
give a db design sample
0
 
LVL 7

Accepted Solution

by:
Saoi earned 500 total points
Comment Utility
For a quite rudimentry search engine, first I'd specify that the search terms must be seperated with a " " (space)

if($SearchTerms){
  $Keywords=explode(" ", $SearchTerms);
  foreach($Keywords as $Keyword){
    // run your sql search query (some thing that returns the ID of the record it matches) and add results to an array for handling later
    // this is likely to return the same result more than once so I would increment the value of the array variable with every result
    // (therefore the results with MORE of the words will have a higher number, allowing us to show results with a higher relevance.
    $Resource=mysql_query("SELECT ID FROM my_search_table WHERE my_search_field LIKE '%$Keyword%'");
    while($Results=mysql_fetch_array($Resource)){
      $SearchResults[$Results['ID']]+=1;
  }
}

//The contents of $SearchResults should look something like this:
Array
(
      [3] => 1  // Article ID 3 in the table matched one word in the search
      [15] => 5 // Article ID 15 matched 5 times
      [27] => 2 // matched 2 times
 )

Its then a fairly simle case of using that array to display your output. Hope that helps!

Saoi
0
 
LVL 32

Expert Comment

by:ldbkutty
Comment Utility
0
 
LVL 1

Author Comment

by:bliesveld
Comment Utility
Saoi  - this looks like what I am looking for... I am still learning php though? How exactly do I extract the data out of your $SearchResults Array?
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now