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
https://forums.phpfreaks.com/topic/72298-query-seperate-words/
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
https://forums.phpfreaks.com/topic/72298-query-seperate-words/#findComment-364545
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
https://forums.phpfreaks.com/topic/72298-query-seperate-words/#findComment-364549
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
https://forums.phpfreaks.com/topic/72298-query-seperate-words/#findComment-364565
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
https://forums.phpfreaks.com/topic/72298-query-seperate-words/#findComment-365008
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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