Monday, July 25, 2011

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 'example string' at line X

PHP/MySQL syntax errors

When you get this error its mostly likely because you misspelled something in the query string when you call the function

mysql_query($MyQueryString);

When I was making an example catalog I was taking input from a form and putting into a table in an SQL database. My query was written as so

$query= 'INSERT INTO catalog (ProductName,Price,Category,SubCategory,Description)
VALUES("'.$_POST['ProductName'].'","'.$_POST['Price'].'","'.$_POST['Category'].'","'.$_POST['SubCategory'].'",'.$_POST['Description'].') ';

If you look at the last variable ($_POST['Description']) its not enclosed in double quotes. It's hard to catch because of all the single and doubles scattered across the query. When I echoed out the query I saw that it read

$query= 'INSERT INTO catalog (ProductName,Price,Category,SubCategory,Description)
VALUES("thename","thePrice","TheCategory","theSub",Description)';

It can be hard to spot since most editors will not catch syntax errors inside quotation marks nor will they highlight the diffident parts of the syntax. If you get this error guess and test till you find your error; its most likely a typo.