Skip to content
Advertisement

Why is my prepared statement causing this syntax error?

When I try to execute a query like this:

$sql3 = "SELECT items, amount FROM todoORtobuy
         WHERE items , amount = :items, :amount
         ORDER BY id DESC LIMIT 40";
$kysely3 = $DBH->prepare($sql3);
$kysely3->execute($data3);

I get this error:

Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ‘ amount = ‘Et dolores et sunt quae officia aut voluptas asperiores optio id e…’ at line 1 in /home2-1/n/noorja/public_html/AdvancedTodoList/TodoTobuy.php:37 Stack trace: #0 /home2-1/n/noorja/public_html/AdvancedTodoList/TodoTobuy.php(37): PDOStatement->execute(Array) #1 {main} thrown in /home2-1/n/noorja/public_html/AdvancedTodoList/TodoTobuy.php on line 37

How can I fix the syntax error?

Advertisement

Answer

This WHERE clause doesn’t make sense:

WHERE items , amount = :items, :amount

It’s basically saying:

Where items, and where amount equals :items, and where :amount

I suspect you meant this:

WHERE items = :items AND amount = :amount

Programming languages are not natural human languages. There’s no intuition and no room for ambiguity. On a WHERE clause you need each individual logical operation to be complete and need to combine them with logical operators (like AND or OR) into other complete operations.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement