"bindParam" 在 "Limit" 之后的多参数不能在 php 中工作?

"bindParam" with multi-parameters after "Limit" couldn't work in php?

我遇到过这个问题,找不到解决方法, 有专家可以帮我吗?

public  function getComments($postId,$pageCmt){
        $db = $this->dbConnect();
        $cmtNb=3;
        $r=($pageCmt-1)*3;

        $req = $db->prepare('SELECT id, comments FROM comments WHERE post_id = ? ORDER BY ind_date DESC,date_comments DESC LIMIT ? OFFSET ?' );

        $req->bindParam(1,$postId,\PDO::PARAM_INT);         
        $req->bindParam(2,$cmtNb,\PDO::PARAM_INT );                 
        $req->bindParam(3,$r,\PDO::PARAM_INT);

         $req->execute(array($postId,$cmtNb,$r));

         return $req;
      }

看来问题出在LIMIT后面的参数上, 因为如果我取消“LIMIT?OFFSET?”代码可以与第一个参数 $postId 一起使用,将获取评论,

但是如果我在页面上添加带有 "LIMIT ? OFFSET?" 的代码,将不会获取评论但也不会显示任何错误,

有什么想法吗?

谢谢。

在这种情况下,您可以尝试将这些值直接连接到查询中,因为用户无法伪造该值,这些值是在服务器端设置的。

此外,您不需要使用 bindParam() 并将值传递给 execute()

你的代码可以修改为

$req = $db->prepare('SELECT id, comments FROM comments WHERE post_id = ? ORDER BY ind_date DESC,date_comments DESC LIMIT {$cmtNb} OFFSET {$r}' );
$req->execute(array($postId));

如果您需要使用用户为 LIMITOFFSET 指定的值,您需要在服务器端验证是否应在 运行 之前接受该值查询。

我今天得到了解决方案:

仍然使用“LIMIT?OFFSET?”然后:

      $req->bindValue(1,$postId,\PDO::PARAM_INT);          
      $req->bindValue(2,$cmtNb,\PDO::PARAM_INT );               
      $req->bindValue(3,$r,\PDO::PARAM_INT);
      var_dump($r);

      $req->execute(); 

今天完美运行!谢谢你们。