Julian Posted August 7, 2009 Share Posted August 7, 2009 Hello, I'm I'm dealing with a simple tags search on my website. Each tag is stored separated by a comma in the same column. (pool, ball, dinner) I'm working with this script that actually do the trick, but I'm facing a MySQL error: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'tags LIKE '%test%' ORDER BY fecha ASC' at line 1" Here's the code: $q = $row_noticias['tags']; //bit of sanitizing $qtrim = strtolower($q); $qtrim = stripslashes($q); $qtrim = strip_tags($q); $qtrim = trim($q); //split values by whitespace into an array, then for each array member stick them together with "OR columnname LIKE '%$arrayvalue%' " $qtrim = explode(',', $q); foreach($qtrim as $value) { $qtrim = $qtrim."OR tags LIKE '%".$value."%' "; } //we now have a string which has the following value: //OR fullname LIKE '%".$value1."%' OR fullname LIKE '%".$value2."%' //we now need to remove the first OR, so the string looks like this: //fullname LIKE '%".$value1."%' OR fullname LIKE '%".$value2."%' $qtrim = substr($qtrim,2); //then we use the string in a mysql query //$query = "SELECT * FROM products WHERE ".$qtrim; $query_relacionados = "SELECT * FROM noticias WHERE " .$qtrim. " ORDER BY fecha ASC"; $relacionados = mysql_query($query_relacionados, $rcpc) or die(mysql_error()); Thanks for your help!!!! Quote Link to comment https://forums.phpfreaks.com/topic/169279-solved-search-using-like-in-mysql/ Share on other sites More sharing options...
kickstart Posted August 7, 2009 Share Posted August 7, 2009 Hi You seem to be doing a foreach around an array, but then overwriting the array with a string in the foreach. Try this:- //split values by whitespace into an array, then for each array member stick them together with "OR columnname LIKE '%$arrayvalue%' " $qtrim = explode(',', $q); $likeString = ""; foreach($qtrim as $value) { $likeString .= "OR tags LIKE '%".$value."%' "; } //we now have a string which has the following value: //OR fullname LIKE '%".$value1."%' OR fullname LIKE '%".$value2."%' //we now need to remove the first OR, so the string looks like this: //fullname LIKE '%".$value1."%' OR fullname LIKE '%".$value2."%' $likeString = substr($likeString,2); //then we use the string in a mysql query //$query = "SELECT * FROM products WHERE ".$qtrim; $query_relacionados = "SELECT * FROM noticias WHERE $likeString ORDER BY fecha ASC"; $relacionados = mysql_query($query_relacionados, $rcpc) or die(mysql_error()); All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/169279-solved-search-using-like-in-mysql/#findComment-893270 Share on other sites More sharing options...
Julian Posted August 7, 2009 Author Share Posted August 7, 2009 Thanks Keith I got rid the error message, working now. Test the script but I found that the results are not as expected. Practical example: On record 1, I have tags: "test, prueba, example". On record 2, I have tags: "test" When display record 2, results are as expected. query shows anything related to "test". On the other hand record 1 only show itself. Thanks again... Quote Link to comment https://forums.phpfreaks.com/topic/169279-solved-search-using-like-in-mysql/#findComment-893280 Share on other sites More sharing options...
Julian Posted August 7, 2009 Author Share Posted August 7, 2009 Never mind.... I have to trim white space!!! It works now. Thank you very much!!! Quote Link to comment https://forums.phpfreaks.com/topic/169279-solved-search-using-like-in-mysql/#findComment-893283 Share on other sites More sharing options...
kickstart Posted August 7, 2009 Share Posted August 7, 2009 Hi Might be the spaces after the commas. Might be worth trimming $value as you use it in $likeString. EDIT - All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/169279-solved-search-using-like-in-mysql/#findComment-893284 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.