Jump to content

QUERY Seperate Words


Canman2005

Recommended Posts

Hi all

 

I have a database table called "content" which im trying to search through using a simple QUERY.

 

I have a HTML form with a form field called "q", I then run a query when the form is submitted which looks like

 

$sql = "SELECT * FROM content WHERE title LIKE '%".$_GET['q']."%' OR content LIKE '%".$_GET['q']."%'";

 

Which searches two fields of the database, "title" and "content".

 

At the moment, if you search for something like

 

"public" or "information" it returns a result, which is great, but if you look for "public information", then it returns nothing.

 

How can I adjust the QUERY so that if someone enters "public information", then it would treat each word (ie: "public" and "information") as a seperate word and look for each one, thus returning a result.

 

Can anyone help?

 

Thanks in advance

 

Dave

Link to comment
Share on other sites

You could try a foreach. explode search criteria by spaces, the add the search with a foreach:

<?php
$q = explode(' ', $_GET['q']);

$sql = "SELECT * FROM content WHERE ";
       foreach($q as $k => $v){
              $sql .= "title LIKE '%".$v."%' ";
       }
$sql .= "OR ";
       foreach($q as $k => $v){
              $sql .= "content LIKE '%".$_GET['q']."%'";
       }
?>

not tested but should work..

Link to comment
Share on other sites

what dragen has will work, from what i can gather. change this loop though:

 

       foreach($q as $k => $v){
              $sql .= "content LIKE '%".$_GET['q']."%'";
       }

 

to this:

 

       foreach($q as $k => $v){
              $sql .= "content LIKE '%".$v."%'";
       }

 

i think. :D

Link to comment
Share on other sites

Hi

 

That look like the start of it, so thanks, but when I run that QUERY on the phase "public information", I get

 

SELECT * FROM content WHERE title LIKE '%public%' title LIKE '%information%' OR content LIKE '%public%'description LIKE '%information%'

 

I think its missing an OR somewhere.

 

Any ideas how I can adjust that?

 

Thanks

Link to comment
Share on other sites

oh sorry, forgot to add the ORs..

<?php
$q = explode(' ', $_GET['q']);
$i = count($q);

$sql = "SELECT * FROM content WHERE ";
       $a = 0;
       foreach($q as $k => $v){
              $sql .= "title LIKE '%".$v."%' ";
              if($i != $a){
                     $sql .= "OR ";
              }
              $a++;
       }
$sql .= "OR ";
       $a = 0;
       foreach($q as $k => $v){
              $sql .= "content LIKE '%".$_GET['q']."%'";
              if($i != $a){
                     $sql .= "OR ";
              }
              $a++;
       }
?>

Give that a try.

There's probably a shorter way of doing it, but it's late and I'm tired ;)

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.