MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 21st, 2000, 03:10 PM
earle earle is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2000
Location: 153 Taylor St
Posts: 2 earle User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Need more than a simple single keyword query into MySQL database.

Need to perform a multiple keyword query in multiple MySQL database fields. How do I do that?

Thnaks,
Ed

Reply With Quote
  #2  
Old January 24th, 2000, 02:49 AM
PAV PAV is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 1999
Location: Netherlands
Posts: 77 PAV User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
Hi Ed,

Can you give us an example of what you want?

Peter

Reply With Quote
  #3  
Old January 24th, 2000, 06:58 AM
earle earle is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2000
Location: 153 Taylor St
Posts: 2 earle User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Yes, here are a couple of examples:
* ProLiant 8500 Quickspecs
* Linux applications
* AlphaServer storage adapters
* multiple keyword query

Reply With Quote
  #4  
Old January 25th, 2000, 03:00 AM
PAV PAV is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 1999
Location: Netherlands
Posts: 77 PAV User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
Hi Ed,

Sorry, I was a bit fuzzy yesterday. Got the clue now.

One way to solve your problem is to have a form field in which visitors can put in more than one word (e.g. the altavista style). You can also use a multiple select-box, but I presume you want people to fill in the words themselves.
Like: <INPUT TYPE="TEXT" NAME="SearchWords">

Let's say you want them to type the words seperated by a space (" ").

The submitted result returns a string variable named $SearchWords containing >=0 words.
Then you have to separate the words again and check them against the database field. You also have to decide whether the words are in an AND or OR clause.
Let's assume you use only the AND clause, you could do:

//Putting all individual words in an array
if ($SearchWords)
{
$SearchWords = trim($SearchWords); // delete surrounding spaces
$SearchWord = explode(" ", $SearchWords); // make array of individual words
}

//Definition of the query
selectquery = "SELECT * FROM table_name";

//Cycle through the words-array if there are word(s) filled in
if (isset($SearchWord))
{
selectquery .=" WHERE ";
for($i = 0; $i < count($SearchWord); $i++)
{
$selectquery .= "search_column LIKE '%" . $SearchWord[$i] . "%'";
if ($i < count($SearchWord)-1)
{
$selectquery .= " AND ";
}
}
}

Succes, Peter




[This message has been edited by PAV (edited January 25, 2000).]

Reply With Quote
  #5  
Old January 25th, 2000, 03:32 AM
asdf1237 asdf1237 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2000
Posts: 6 asdf1237 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi Peter,

I'm trying to do the same thing with 3 columns (any one of the 3 columns can have the search words in it). Like, if someone does a search for "big red dog", it will look through the columns and it will show records that contains the words the user submitted. The words can be in any order. Such as the following records:

"that is a big and red dog"
"big and red dogs are mean!"
"i hate big, red dogs"
"my name is red big dog"
etc. etc.

Any idea on how to do this? I'm not very good at PHP yet to do this.

Also, what is "isset"?

Also, is it possible for one column to have one of the words and another column can contain the other 2.

[This message has been edited by asdf1237 (edited January 25, 2000).]

[This message has been edited by asdf1237 (edited January 25, 2000).]

Reply With Quote
  #6  
Old January 25th, 2000, 05:12 AM
PAV PAV is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 1999
Location: Netherlands
Posts: 77 PAV User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
Hi Ed,

The isset() function checks whether or not a variable is set. I.E. the $SearchWord array is only set when there is a value in $SearchWords.

From your comment I presume you want to use the OR clause (word(s) in one OR more columns).
Just rewrite the code, to cycle through the 3 colums, as follows:

//Make array of the columns you want to search in
$column = ("column_name1", "column_name2", "column_name3");

//Cycle through the words-array if there are word(s) filled in
if (isset($SearchWord))
{
selectquery .=" WHERE ";
// round 1: cycle through the columns
for($j = 0; $j < count($column); $j++)
{
//Round 2: within column cycle through word(s)
for($i = 0; $i < count($SearchWord); $i++)
{
$selectquery .= "$column[$j] LIKE '%" . $SearchWord[$i] . "%'";
if ($i < count($SearchWord)-1)
{
$selectquery .= " OR ";
}
}
if ($j < count($column)-1)
{
$selectquery .= " OR ";
}
}
}

Give it a try and tell me if this works.

Peter


Reply With Quote
  #7  
Old January 25th, 2000, 11:14 AM
asdf1237 asdf1237 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2000
Posts: 6 asdf1237 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Nope, not working. I'm getting parse errors. I think this line is causing problems:

$column = ("column_name1", "column_name2", "column_name3");

Reply With Quote
  #8  
Old January 25th, 2000, 04:30 PM
asdf1237 asdf1237 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2000
Posts: 6 asdf1237 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hey, I got it working. I forgot to copy 2 lines and you forgot a $ It works great, but I was wondering how would I modify it so that if I did a search for "big dog" and in column 1 was the word "big" and column 2 had "dog", it would return results like this, too?

Reply With Quote
  #9  
Old January 26th, 2000, 02:27 AM
PAV PAV is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 1999
Location: Netherlands
Posts: 77 PAV User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
I think it should return a result when BIG is in column 1 and DOG in column 2, because of the OR clause.
The best thing to do (if you are familiar with SQL), is to first get the right SQL syntax and then build the PHP application to fill it.
Just play around!

Peter

Reply With Quote
  #10  
Old January 26th, 2000, 03:10 AM
asdf1237 asdf1237 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2000
Posts: 6 asdf1237 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Nope, it doesn't work if one word is in one column and the other is in another. Do you tink you know what's wrong? Thanks for the help so far though

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > How To: multiple keyword query in multiple database fields


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway