pdo绑定没有给予价值

时间:2012-10-10 19:38:09

标签: php pdo

:cat_id的值为9并且:如果我手动输入它们,则tlk_id的值为3它将以所需的结果运行但是在绑定它们之后它会给我一个零行数

<?

   $sql = ("
       SELECT
           p.post_id as sn,
           p.subject,
           p.username,
           COUNT(thread_id)-1 AS responses,
           MAX(DATE_FORMAT(p.date_posted,'%e-%b-%y %l:  %i    %p')) AS first,
           MAX(DATE_FORMAT(t.date_posted,'%e-%b-%y %l:%i %p')) AS last
       FROM
           posts AS p
       INNER JOIN
           threads AS t 
       USING 
           (post_id) 
       INNER JOIN 
           register AS u 
       ON
           p.username = u.username
       WHERE 
           t.category_id =:cat_id
       AND
           p.category_id =:cat_id
       AND
           t.forum_id = :tlk_id
       AND
           p.forum_id = :tlk_id
       GROUP BY
           (t.post_id)
       ORDER BY
           last DESC
       LIMIT $start,$display
    ");

    $sth = $dbh->prepare($sql);
    $sth->bindParam(':tlk_id',$tlk_id);
    $sth->bindParam(':cat_id',$cat_id);

    $r =  $sth->execute();
    $count = $sth->rowCount();
    echo $count;
?>

2 个答案:

答案 0 :(得分:0)

如果你这样做会发生什么:

$tlk_id = 3;
$cat_id = 9;

$sth = $dbh->prepare($sql);
$sth->bindParam(':tlk_id',$tlk_id);
$sth->bindParam(':cat_id',$cat_id);

我将值赋给变量,因为bindParam期望引用作为第二个参数。

答案 1 :(得分:0)

从错误:

  

参数号无效:参数...

您可以看到并非所有参数都已处理。所以在这种情况下你可以添加4个参数:

$sql = "SELECT 
        .....
       WHERE 
           t.category_id =:cat_id_1
       AND
           p.category_id =:cat_id_2
       AND
           t.forum_id = :tlk_id_1
       AND
           p.forum_id = :tlk_id_2
      ....
    ";

    $sth = $dbh->prepare($sql);
    $sth->bindParam(':tlk_id_1',$tlk_id);
    $sth->bindParam(':tlk_id_2',$tlk_id);
    $sth->bindParam(':cat_id_1',$cat_id);
    $sth->bindParam(':cat_id_2',$cat_id);