-1

I am having an issue with PDO which I have seen discussed around but the different solutions provided did not work for me. It is when binding a number to set the LIMIT of a SQL query.

Here is the error I get:

Warning: PDOStatement::execute(): SQLSTATE[42000]: Syntax error or access violation: 1064 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 ''2'' at line 1 in ...

The code:

$remaining = 3 - $countRows;
$con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
$result4 = $con->prepare("SELECT  * FROM  item_descr WHERE id_item != ? LIMIT ?");
$result4->execute(array($itemId, intval($remaining)));
samyb8
  • 2,560
  • 10
  • 40
  • 68

2 Answers2

2
$dbh->setAttribute( PDO::ATTR_EMULATE_PREPARES, false );

Adding this line and removing the intval( was the solution, as suggested here:

Answer to a similar question

Community
  • 1
  • 1
samyb8
  • 2,560
  • 10
  • 40
  • 68
0

Why dont you do something like:

SELECT  * FROM  item_descr WHERE id_item != :id LIMIT :limit

$result->bindParam(':id', $itemID, PDO::PARAM_INT);
$result->bindParam(':limit', $remainint, PDO::PARAM_INT);

with your current code you are not really binding parameters.

GGio
  • 7,563
  • 11
  • 44
  • 81