猿问

计算由 Codeigniter -> 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

我如何从上面给出的代码中得到 Total = 70,非常感谢


心有法竹
浏览 148回答 4
4回答

桃花长相依

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

一只萌萌小番薯

您可以使用此查询:select&nbsp;SUM(table_rows)&nbsp;as&nbsp;'Count'&nbsp;from&nbsp;information_schema.tables&nbsp; where&nbsp;&nbsp;table_schema='MyDb'&nbsp;AND&nbsp;TABLE_NAME&nbsp;IN&nbsp;('table1',...)

慕妹3146593

您可以通过两种方式进行操作,首先您必须进行简单的编码,您可以在下面看到:$tables =&nbsp; $this->db->list_tables();&nbsp; // given all table from data base$counted_data = 0;&nbsp; &nbsp;foreach ($tables as $key => $value) {&nbsp; &nbsp; &nbsp; &nbsp;$val =&nbsp; $this->db->count_all($value);&nbsp; // count all data from related table&nbsp; &nbsp; &nbsp; &nbsp;$counted_data += $val;&nbsp; // add to total count&nbsp; &nbsp;}echo $counted_data;&nbsp; // desire reult you want

当年话下

希望这有助于num_rows()函数&nbsp; &nbsp; $result = $this->db->get('TABLE_NAME');&nbsp;&nbsp; &nbsp; echo $result->num_rows(); // To count result&nbsp;&nbsp; &nbsp; print_r($result->result()); // To get total data&nbsp;
随时随地看视频慕课网APP
我要回答