Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.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 从另一个表中计算行数_Php_Mysql_Count_Row - Fatal编程技术网

Php 从另一个表中计算行数

Php 从另一个表中计算行数,php,mysql,count,row,Php,Mysql,Count,Row,我有这个桌子结构 sites id | name | etc 1 | test | etc 2 | test2 | etc 3 | test3 | etc comments id | site_id | comment 1 | 2 | test comment 2 | 3 | test2 comment 3 | 2 | test3 comment 我想选择列出所有网站 例如 我有这个代码来显示网站。 谁能告诉我如何计算注释表的行

我有这个桌子结构

sites
id | name  | etc
1  | test  | etc
2  | test2 | etc
3  | test3 | etc

comments
id | site_id | comment
1  |    2    |   test comment
2  |    3    |   test2 comment
3  |    2    |   test3 comment
我想选择列出所有网站 例如

我有这个代码来显示网站。 谁能告诉我如何计算注释表的行数

$subcat_id = $_GET["id"];


        $stmt = $handler->prepare("SELECT *
                                    FROM sites sites
                                    LEFT JOIN comments comments ON ( sites.id = comments.site_id )
                                    WHERE sites.subcat_id = '$subcat_id' AND sites.status = 1
                                    GROUP BY sites.id
                                    ORDER BY RAND()");
        $stmt->execute();
        $no=$stmt->rowCount(); 
        while($row = $stmt->fetch(PDO::FETCH_OBJ)){
            $sites[]=$row;
        }

        $smarty->assign('site',$sites);
        $smarty->assign('anzahl',$no);

        $smarty->display('subcat.tpl');

稍微重写sql:

SELECT *
FROM sites sites
LEFT JOIN comments comments ON ( sites.id = comments.site_id )
WHERE sites.subcat_id = '$subcat_id' AND sites.status = 1
ORDER BY sites.id

现在您知道每个站点都会有连续的注释,您可以在php中检查是否有一个新站点需要打印标题。

稍微重写sql:

SELECT *
FROM sites sites
LEFT JOIN comments comments ON ( sites.id = comments.site_id )
WHERE sites.subcat_id = '$subcat_id' AND sites.status = 1
ORDER BY sites.id

现在,您知道每个站点都会有连续的评论,您可以在php中检查是否有新站点,您应该为其打印标题。

是否可以在选择中计数,我可以进行新选择我知道。但选择必须在同一时间,因为结果将转到smarty并列出。。。当我把评论的选择分开,然后我就没有结果了,可以在选择中计数,我可以做一个新的选择,我知道。但选择必须在同一时间,因为结果将转到smarty并列出。。。当我在这篇文章中单独选择评论时,我就没有结果了