15
A few days ago, I stopped using the functions mysql_*
(already obsolete), and I switched to PDO
. I have a function that does query database, but I’m having some problems using the LIMIT
with Prepared statements.
Here’s the mistake I get:
Fatal error: Uncaught Exception 'Pdoexception' with message '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 ''0', '4' line 1' in /home/yuran/public_html/avec/Databasecontent.php:30 Stack trace: #0 /home/yuran/public_html/avec/Databasecontent.php(30): Pdostatement->execute(Array) #1 /home/yuran/public_html/avec/inc/header.php(43): Databasecontent->fetchSomeRows(Object(PDO), 'Topics', 'topic_id', 'DSC', 0, 4) #2 /home/yuran/public_html/avec/index.php(7): require_once('/home/yuran/pub...') #3 {main} thrown in /home/yuran/public_html/avec/Databasecontent.php on line 30
This is the code of my function.
<?php
private $sql = "SELECT * FROM ";
public function fetchSomeRows($conn, $table, $rowOrder, $direction, $initialLimit, $finalLimit)
{
$this->sql .= "{$table} ORDER BY :roworder :direction LIMIT :initiallimit, :finallimit";
$q = $conn->prepare($this->sql);
$q->execute(array(':roworder' => $rowOrder,':direction'=>$direction,':initiallimit'=>$initialLimit,':finallimit'=>$finalLimit));
$q->setFetchMode(PDO::FETCH_ASSOC);
return $q;
}
?>
In fact you could have switched to the functions
mysqli
that are almost identical and solved your problem more simply and without trauma.– Maniero
You must inform another part of the code from which the property comes
$this->sql
. Because the exception reports a syntax error– Wallace Maxters
@rray in the case of table names is true that it is not possible to bind so I used {$table} but I didn’t know the same thing happened with LIMIT.
– Yuran Pereira
I made a mistake, so I removed the comment, I tested the response of @André Ribeiro and it worked, living and learning :)
– rray