I've got a simple SQL query that keeps throwing an SQL syntax error. It looks like this:
$sql = "SELECT COUNT(*) AS TOTAL FROM PRODUCT WHERE ID_PRODUCT = ".$id;
$result = mysql_query($sql) or die(mysql_error());
Which throws :
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 'WHERE
ID_PRODUCT = 1' at line 1
I don't understand what's going on. Of course if I try the query directly in phpMyAdmin it works with no problem.
It returns exactly the same error with a SELECT *
Edit: ID_PRODUCT is a foreign key...
Update: I also get the same error when replacing the $id variable by a static value in the $sql string WHERE ID_PRODUCT = 2
and when protecting the names by quotes.
Copyright Notice:Content Author:「Jla」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/2310914/simple-query-throwing-an-sql-syntax-error