计算由代码点火器 -> list_tables() 生成的多个表中的总行数



需要建议计算我从Codeigniter list_tables 生成的总行数

$sqlx = $this->db->list_tables();
foreach ($sqlx as $table) { 
$counteachrow = $this->db->count_all($table);
echo $table; | echo $counteachrow; }
//Result for each table
| Name | Count |
-----------------
| TblA |  25   |
| TblB |  25   |
| TblC |  20   |
----------------
Total = 70 //Expected value

如何从上面给出的代码中获得总计=70,非常感谢

$sqlx = $this->db->list_tables();
$html = "<table>
<tr>
<th>Table Name</th>
<th>Rows</th>
</tr>";
$finalCount = 0;
foreach ($sqlx as $table) {                 
$counteachrow = $this->db->count_all($table);                    
$html .= "<tr>
<td>".$table."</td>
<td>".$counteachrow."</td>
</tr>";  
$finalCount += $counteachrow;
}
$html .= "</table><br/>";
echo $html;
echo "Total = ".$finalCount."<br/>";
exit;

您可以从两种方式完成,首先您必须进行简单的编码,您可以在下面看到:

$tables =  $this->db->list_tables();  // given all table from data base
$counted_data = 0;
foreach ($tables as $key => $value) {
$val =  $this->db->count_all($value);  // count all data from related table
$counted_data += $val;  // add to total count
}
echo $counted_data;  // desire reult you want

希望它对你有用。

希望这有助于num_rows((函数

$result = $this->db->get('TABLE_NAME'); 
echo $result->num_rows(); // To count result 
print_r($result->result()); // To get total data 

您可以使用以下查询:

select SUM(table_rows) as 'Count' from information_schema.tables 
where  table_schema='MyDb' AND TABLE_NAME IN ('table1',...)

相关内容

  • 没有找到相关文章

最新更新