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 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动