Canman2005 Posted October 8, 2007 Share Posted October 8, 2007 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 More sharing options...
Dragen Posted October 8, 2007 Share Posted October 8, 2007 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 More sharing options...
JasonLewis Posted October 8, 2007 Share Posted October 8, 2007 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. Link to comment https://forums.phpfreaks.com/topic/72298-query-seperate-words/#findComment-364549 Share on other sites More sharing options...
Canman2005 Posted October 8, 2007 Author Share Posted October 8, 2007 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 More sharing options...
Canman2005 Posted October 8, 2007 Author Share Posted October 8, 2007 Anyone? Link to comment https://forums.phpfreaks.com/topic/72298-query-seperate-words/#findComment-364586 Share on other sites More sharing options...
Dragen Posted October 8, 2007 Share Posted October 8, 2007 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 More sharing options...
Barand Posted October 8, 2007 Share Posted October 8, 2007 Have you considered FULL TEXT Link to comment https://forums.phpfreaks.com/topic/72298-query-seperate-words/#findComment-365012 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.