Jump to content

You have an error in your SQL syntax


BryantA

Recommended Posts

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.

 

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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);
}

Link to comment
Share on other sites

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!  :D

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.