dpf25323 2012-12-13 13:55
浏览 90
已采纳

PHP PDO语句在SQL语句中将参数传递给LIMIT似乎不起作用

For some reason, I can't get this to work. It returns the right amount of rows but the data seems to be empty, no matter what I try:

$page = intval(trim($_GET['p']));
if($page <= 0)
    $page = 1;
$showcount = 15;
$limit_start = (int)(($page-1)*$showcount);

$stmt_scorelist = $dbh->prepare("
    SELECT user_id,min_time 
    FROM scoretable 
    WHERE type_id = :type AND level_id = :level_id 
    GROUP BY user_id 
    ORDER BY MIN(min_time) ASC 
    LIMIT :limit_start, :showcount
");
$stmt_scorelist->bindParam(':type', $type);
$stmt_scorelist->bindParam(':level_id', $level_id);
$stmt_scorelist->bindValue(':limit_start', (int)$limit_start, PDO::PARAM_INT);
$stmt_scorelist->bindValue(':showcount', (int)$showcount, PDO::PARAM_INT);

I've tried to enter numbers explicitly like this:

$stmt_scorelist->bindValue(':limit_start', 0, PDO::PARAM_INT);
$stmt_scorelist->bindValue(':showcount', 15, PDO::PARAM_INT);

It always returns the correct amount of rows, but the data seems to be empty. I've also tried both (int)$var and intval(trim($var)), I've tried to use bindParam() instead of bindValue(), but none works.

The one thing that works, is if I comment the bound values and replace them with pure numbers in the statement, it works just as intended, the right amount of rows and the data returns:

$stmt_scorelist = $dbh->prepare("
    SELECT user_id,min_time 
    FROM scoretable 
    WHERE type_id = :type AND level_id = :level_id 
    GROUP BY user_id 
    ORDER BY MIN(min_time) ASC 
    LIMIT 0, 15
");
$stmt_scorelist->bindParam(':type', $type);
$stmt_scorelist->bindParam(':level_id', $level_id);
//$stmt_scorelist->bindValue(':limit_start', 0, PDO::PARAM_INT);
//$stmt_scorelist->bindValue(':showcount', 14, PDO::PARAM_INT);

What am I doing wrong? I've seen multiple people have the same issues, but none of the solutions they found seems to work for me.

  • 写回答

1条回答 默认 最新

  • duanniedang3946 2012-12-21 08:21
    关注

    This solved the issue:

    $dbh->setAttribute( PDO::ATTR_EMULATE_PREPARES, false );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog