Simple query throwing an SQL syntax error
NickName:Jla Ask DateTime:2010-02-22T20:50:14

Simple query throwing an SQL syntax error

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

Answers
MightyE 2010-02-22T13:18:55

Try completely retyping your $sql line by hand, you might accidentally have an invisible extended character in there (such as a non-breaking space instead of a regular space, etc).",


brabster 2010-02-22T12:54:22

If the ID_PRODUCT column is a varchar, you will need to put single quotes around the value in your where clause. Might be something like that, can't tell without info about your schema.\n\n\n\nUpdate: Not sure then. Obvious next step is to print out the generated SQL and try running it manually, hopefully the issue will manifest and you will be able to rule out PHP as the source of the issue.",


More about “Simple query throwing an SQL syntax error” related questions

Simple query throwing an SQL syntax error

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(

Show Detail

Simple transaction query but has syntax error

I am trying some sammple query in mysql client but the transaction syntax does not work] MySQL database-2:3306 ssl TestDb SQL > START TRANSACTION ...

Show Detail

mysql update query throwing syntax error

I am trying to update user information (phone number) but mysql showing syntax error. Here is the query for showing all data (phone number) select distinct a.phone from users u join updated_phone...

Show Detail

SQL query syntax error in PHP

I am trying to do a simple query on my MySQL db and echo back the result. I am getting the following error: Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting ident...

Show Detail

MySQL Error Code: 1064. You have an error in your SQL syntax

Really, what could possibly be wrong?! It does not get any more simple - the entire query: line 1: use foo line 2: line 3: select * from test_table_1; Error Code: You have an error in yo...

Show Detail

Clickhouse create table query throwing syntax error

I am executing the below-given query but it throwing an error. I am new to clickhouse can you please assist me with how to fix it? CREATE TABLE rapidTax.mutualFund ( `id` varchar(255), `

Show Detail

SQL syntax error on nested query

I'm maintaining an old website that is throwing some sql errors on a mysql query. The error goes as follows... You have an error in your SQL syntax; check the manual that corresponds to your MySQL

Show Detail

What is the syntax error in this simple SQL query for creating a table?

I am trying out IntelliJ's DataGrip to do some SQL work on a MariaDB database. Somehow i cannot execute the query that was automatically created by DataGrip itself.... Can you help me find the erro...

Show Detail

MySQL query - weird syntax error

I'm using phpmyadmin and MySQL to run a simple query, that creates a function checking the existence of a certain record. It keeps throwing a syntax error at line 7 with Declare. I have no idea why...

Show Detail

Recieving SQL Syntax error on simple insert query

I have some simple 4 field SQL-Query with Parameters to insert a single record into some logging-table in ms-access database. string SQL = "INSERT INTO logHistory (ip, action, userid, time)" + ...

Show Detail