Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用内部连接php(计数)获取属于post的总行数?_Php_Mysqli - Fatal编程技术网

如何使用内部连接php(计数)获取属于post的总行数?

如何使用内部连接php(计数)获取属于post的总行数?,php,mysqli,Php,Mysqli,我使用内部连接列出帖子及其类别和评论, 像这样: if($stmt = $pdo->prepare("SELECT * FROM posts INNER JOIN categories ON posts.cat_id = categories.cat_id INNER JOIN comments ON posts.post_id = comment_id WHERE user_id = :sid")){ $stmt->execute(arr

我使用内部连接列出帖子及其类别和评论, 像这样:

    if($stmt = $pdo->prepare("SELECT * FROM posts 
    INNER JOIN categories ON posts.cat_id = categories.cat_id
    INNER JOIN comments ON posts.post_id = comment_id 
    WHERE user_id = :sid")){
    $stmt->execute(array('sid'=>$sid));
然后执行while循环以列出它们

while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
usaqe是:

$row['category'];
我想获得属于帖子的评论总行数,并将其显示为: 帖子评论总数:$row['Total_coments'


有可能吗?

您可以这样做,假设注释id是唯一的

if($stmt = $pdo->prepare("SELECT posts.*,(select count(comment_id) from 
comments) 
as total_coments
FROM posts 
INNER JOIN categories ON posts.cat_id = categories.cat_id
INNER JOIN comments ON posts.post_id = comment_id 
WHERE user_id = :sid")){
$stmt->execute(array('sid'=>$sid));

当我像这样使用它时:SELECT posts.*,SELECT countcomment\u id from comments as total\u coments然后它忽略类别,有错误未定义索引:cat\u name有相同的错误,我尝试了这种方式“posts.*,categories.*,comments.*”`现在可以工作了,非常感谢budd。