BryantA Posted June 5, 2012 Share Posted June 5, 2012 Hi, I'm a complete noob. What I'm trying to do is create a search engine for a website that retrieves links from pages that I've placed in a table in my database according to their designated keywords. But every time I enter a keyword into the search box this error comes up: 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 'OR keywords LIKE '%keyword%'' at line 1 Obviously there is something wrong with my MySQL database or table but I'm not sure what exactly it is though. I know I have my collation set to "utf8_unicode_ci" because I heard that that was the best to use. I don't know if that choice of collation is the issue. Do you have any suggestions as to why it might be saying this and what I can do to fix it? Please help. Quote Link to comment Share on other sites More sharing options...
requinix Posted June 5, 2012 Share Posted June 5, 2012 It's your query. The problem is in your query. That's why it says "in your SQL syntax". What's your query? Or rather, what's the code to generate the query? Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted June 5, 2012 Share Posted June 5, 2012 The problem is likely in a conditional statement in your code, posted in your existing thread for this problem. You need to be testing if $i == 1 not if $i == i Quote Link to comment Share on other sites More sharing options...
awjudd Posted June 5, 2012 Share Posted June 5, 2012 Without your query, nobody can help you. ~awjudd Quote Link to comment Share on other sites More sharing options...
smoseley Posted June 5, 2012 Share Posted June 5, 2012 The OP's Code (from other thread) <?php $search = $_GET['search']; $terms = explode(" ", $search); $query = "SELECT * FROM search WHERE "; foreach ($terms as $each) { $i++; if ($i == i) $query .= "keywords LIKE '%$each%' "; else $query .= "OR keywords LIKE '%$each%' "; } // connect mysql_connect("localhost", "username", "password"); mysql_select_db("databasename"); $query = mysql_query($query); Quote Link to comment Share on other sites More sharing options...
smoseley Posted June 5, 2012 Share Posted June 5, 2012 And the fix: <?php $search = $_GET['search']; $terms = explode(" ", $search); if (!empty($terms)) { // connect mysql_connect("localhost", "username", "password"); mysql_select_db("databasename"); $query = "SELECT * FROM `search`"; foreach ($terms as &$term) { $term = mysql_real_escape_string($term); $term = "`keywords` LIKE '%{$term}%'"; } $terms= implode(" OR ", $terms); $query .= " WHERE {$terms}"; $query = mysql_query($query); } Quote Link to comment Share on other sites More sharing options...
BryantA Posted June 5, 2012 Author Share Posted June 5, 2012 OMG!!! It's fixed. I finally have a working search engine for my site. Now all I have to do is make it look pretty. @requinix My query was one of the issues. I had it named wrong. @PFMaBiSmAd Great observation. I didn't think that was an issue but it was. @smoseley Your revision allowed me to switch everything around how it should be. Thank you ya'll so much. Overall. You guys saved me sssoooooo much time you have no idea. One day when I learn PHP maybe I can return the favor...one day! Quote Link to comment Share on other sites More sharing options...
smoseley Posted June 5, 2012 Share Posted June 5, 2012 You're very welcome, Bryant. Pay it forward! Quote Link to comment 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.