I'm currently learning php
and I'm using PDO
to handle my database. While trying to use LIMIT
in a query i encountered a problem:
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 ''0', '25'' at line 1
After some 'research' the solution was to add this attribute to my PDO
:
PDO::ATTR_EMULATE_PREPARES => false
Now my script looks like this(and the LIMIT
works):
try {
$attr = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_EMULATE_PREPARES => false
];
$pdo = new PDO('mysql:host=127.0.0.1;dbname=db_name', 'root', '', $attr);
} catch (PDOException $e) {
exit($e->getMessage());
}
$rows = $pdo->prepare("SELECT * FROM table_name LIMIT ?, ?");
$rows->execute([0, 25]);
But my question is what does PDO::ATTR_EMULATE_PREPARES => false
actually do? And is it a good solution? Does it affect in any way shape or form the level of security of that prepared statement? Is there another solution that might do a better job when it comes to using LIMIT
within a prepared statement?
Thank you! :D