douweicheng5532 2014-05-19 02:43
浏览 57
已采纳

无法将INT绑定到PDO为LIMIT和OFFSET准备的语句[重复]

This question already has an answer here:

I'm having trouble binding an INT value to my PDO prepared statement. It's passing the INTs as strings, ''s and all. I need to bind INTs for my LIMIT and OFFSET in the SQL query.

Here's my code:

$sth = $dbh->prepare("SELECT * FROM post WHERE private = 0 ORDER BY post_time DESC LIMIT :number_results OFFSET :offset");
$sth->execute(array(':number_results' => $number_results, ':offset' => $offset));
$errors = $sth->errorInfo();
    print_r($errors[2]);

I'm receiving 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 ''20' OFFSET '0'' at line 1

I found this bug report, but have not had any luck so far with the comments. How can I bind these values?

Edit: When I try to set the param type, I don't get any errors, but it doesn't look like it's binding the correct parameters?

Params:  2
Key: Name: [15] :number_results
paramno=-1
name=[15] ":number_results"
is_param=1
param_type=1
Key: Name: [7] :offset
paramno=-1
name=[7] ":offset"
is_param=1
param_type=1
</div>
  • 写回答

1条回答 默认 最新

  • dongtuoleng8624 2014-05-19 02:51
    关注

    You can bind parameters and specify the data type using bindParam. In your case you want to tell PDO the value is an integer using PDO::PARAM_INT.

    $sth = $dbh->prepare("SELECT * FROM post WHERE private = 0 ORDER BY post_time DESC LIMIT :number_results OFFSET :offset");
    
    // Bind params
    $sth->bindParam(':number_results', $number_results, PDO::PARAM_INT);
    $sth->bindParam(':offset', $offset, PDO::PARAM_INT);
    
    $sth->execute();
    $errors = $sth->errorInfo();
        print_r($errors[2]); 
    

    Because you've specified the parameters individually, there is no need to pass them through as an array in your $sth->execute(); call.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?