0

: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;
?>
4

2 回答 2

0

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

$tlk_id = 3;
$cat_id = 9;

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

我将这些值分配给一个变量,因为 bindParam 需要一个引用作为第二个参数。

于 2012-10-10T19:50:14.147 回答
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);
于 2012-10-10T19:50:39.823 回答