PDO:无法传递变量来准备

时间:2012-06-09 17:19:45

标签: php mysql pdo

  

可能重复:
  PHP PDO bindValue in LIMIT

    $number=8;
    try {  
        $topics=$dbh->prepare("SELECT * FROM topictable ORDER BY RAND() LIMIT ?");
        $topics->execute(array($number));       
        $topicarray = $topics->fetch(PDO::FETCH_BOTH);
        print_r($topicarray);
    }  
    catch(PDOException $e) {  
        echo $e->getMessage();  
    }

以上是我的代码,我知道问题来自$topics=$dbh->prepare("SELECT * FROM topictable ORDER BY RAND() LIMIT ?"),因为当我改变时?到8,它的工作原理。请赐教。感谢。

1 个答案:

答案 0 :(得分:1)

$topics=$dbh->prepare("...LIMIT :limit");
$topics->bindValue(':limit', intval($number), PDO::PARAM_INT);
$topics->execute();