计算表格的行数

时间:2017-01-04 16:03:41

标签: php mysql

我想计算一个表的行并将结果放在PHP表中。 但是,我有一个小问题。

我的代码:

$reponse = $bdd->query('SHOW TABLES FROM '.$db_name);
    echo '<table class="table table-bordered table-striped ">';
            echo'<thead>
                  <th class="text-center">Name</th>
                  <th class="text-center">Numbers of lines</th>
                  <th class="text-center">Show columns</th>
                  <th class="text-center">Rename</th>
                  <th class="text-center">Delete</th>
                 </thead>';
            echo'<tbody>';
            ?>

    <?php
   while($value = $reponse->fetch())
{
    $table = $value[$i];
    echo '<tr><td class="col-db text-center">'.$table.'</td>';

    $rep = $bdd->query('SELECT COUNT(*) FROM '.$table);
    $data = $rep->fetch();

    var_dump($data, $rep);
    echo'<td class="col-db text-center">'.$data.'</td>';

}

enter image description here

但我的桌子就像这样正常而且没有错误:

enter image description here

1 个答案:

答案 0 :(得分:2)

您不必遍历所有行来执行此操作,您只需使用计数查询:

SELECT count(*) FROM table

此结果中的第一行将包含此表中的行数。