Search code examples
phpmysqlpdopagination

PDO pagination syntax violation error


Can someone please check out whats wrong with my script below.

i'm getting "PHP 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', '1'' at line 1'"

    $limit = 1;
$p=$_GET['p']=="" ? 1:$_GET['p'];

$start=($p-1)*$limit;


$sa_stmt = $db->prepare('SELECT * FROM members WHERE referral=:username ORDER BY memberID LIMIT :start, :limit');


$sa_stmt->execute(array(
                ':username' => $_SESSION['username'],
                ':start' => $start,
                ':limit' => $limit
            ));

while($info = $sa_stmt->fetch(PDO::FETCH_ASSOC)){ 
 $norec = "yes";
echo ''.$info["username"] . '    '; }


$count=$db->prepare("SELECT COUNT(memberID) FROM members WHERE referral=:username ORDER BY memberID");
$count->execute(array(':username' => $_SESSION['username']));
$count=$count->fetchColumn();

$countP=(ceil($count/$limit)) + 1;
$tW=($countP*50) + $countP;
echo"<center style='overflow-x:auto;margin-top:10px;padding-bottom:10px;'>";
 echo"<div style='width:".$tW."px'>";
 for($i=1;$i<$countP;$i++){
  $isC=$i==$_GET['p'] ? "b-green":"";
  echo "<a href='?p=$i'><button class='pgbutton $isC'>$i</button></a>";
 }
 echo"</div>";
echo"</center>";

Solution

  • $sa_stmt = $db->prepare('SELECT * FROM members WHERE referral=:username ORDER BY memberID LIMIT :start, :limit');
    
    
    
        $sa_stmt->bindValue(":username", $_SESSION['username'], PDO::PARAM_INT);
            $sa_stmt->bindValue(":start", $start, PDO::PARAM_INT);
            $sa_stmt->bindValue(":limit", $limit, PDO::PARAM_INT);
            $sa_stmt->execute();
    
        while($info = $sa_stmt->fetch(PDO::FETCH_ASSOC)){ 
         $norec = "yes";
        echo ''.$info["username"] . '    '; }