Solved

Search mysql db with multiple keywords

Posted on 2004-09-06
4
1,062 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 5

Expert Comment

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

Accepted Solution

by:
Saoi earned 500 total points
ID: 11988111
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
ID: 11988671
0
 
LVL 1

Author Comment

by:bliesveld
ID: 11993534
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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

691 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