Simple query throwing SQL syntax error

I have a simple SQL query that continues to throw 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()); 

What throws away:

You have an error in the SQL syntax; check the manual that matches your version of MySQL server for the correct syntax to use next to WHERE ID_PRODUCT = 1 'on line 1

I do not understand what is going on. Of course, if I try the request directly in phpMyAdmin, it will work without problems.

It returns the exact same error with SELECT *

Edit : ID_PRODUCT is a foreign key ...

Refresh . I also get the same error when replacing the variable $ id with a static value in the string $ sql WHERE ID_PRODUCT = 2and when protecting names with quotes.

+3
3

$sql , (, ..).

+2

ID_PRODUCT varchar, where. , - , .


: . - SQL , , , PHP .

+3

, . . FULL-CAPITAL , .

: , , .

:

SELECT COUNT(*) AS "total" FROM "Product" WHERE "id_Product" = 1

Note that MySQL uses a non-standard backtick character (`) to quote them by default. (Although they do not display here correctly, so I could use double quotes.)

0
source

Source: https://habr.com/ru/post/1733878/


All Articles