A few days ago, I stopped using the mysql_*
functions (because they are already obsolete), and switched to PDO
. I have a function that does query the database, but I'm having some problems using LIMIT
with prepared statements .
Here's the error 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 '' at line 1 'in /home/yuran/public_html/avec/DatabaseContent.php:30 Stack trace: # 0 /home/yuran/public_html/avec/DatabaseContent.php(30): PDOStatement-> Run (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 for 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;
}
?>