如何在视图页面中显示mysql数据库表的总大小(Codeigniter)

gcuhipw9  于 2023-02-14  发布在  Mysql
关注(0)|答案(2)|浏览(151)

如何让这段代码在Codeigniter上工作的回声总mysql数据库表大小?

SELECT table_schema "Data Base Name",
sum( data_length + index_length ) / 1024 /
1024 "Data Base Size in MB",
sum( data_free )/ 1024 / 1024 "Free Space in MB"
FROM information_schema.TABLES
GROUP BY table_schema;

这是如何回显总mysql数据库表行数:

<?php
$sqlx = $this->db->list_tables();
$finalCount = 0;
foreach ($sqlx as $table) {                 
   $counteachrow = $this->db->count_all_results($table);                    
   $finalCount += $counteachrow;
}    
?>

<?php echo $finalCount; ?>
wmtdaxz3

wmtdaxz31#

创建新模型

<?php

class Database_model extends CI_Model {

    public function __construct()
    {
        parent::__construct();
    }

    public function get_database_size() {
        $query = "SELECT table_schema 'Data Base Name', sum( data_length + index_length ) / 1024 / 1024 'Data Base Size in MB', sum( data_free )/ 1024 / 1024 'Free Space in MB'
FROM information_schema.TABLES
GROUP BY table_schema";
        $result = $this->db->query($query);
        return $result->result_array();
    }

    public function get_total_rows() {
        $sqlx = $this->db->list_tables();
        $finalCount = 0;
        foreach ($sqlx as $table) {                 
            $counteachrow = $this->db->count_all_results($table);                    
            $finalCount += $counteachrow;
        }
        return $finalCount;
    }
}

以及控制器

$database_size = $this->database_model->get_database_size();
$total_rows = $this->database_model->get_total_rows();
oiopk7p5

oiopk7p52#

public function get_database_size() {

    $sql = "SELECT table_schema AS db_name, round((sum(data_length + index_length) / 1024 / 1024), 2) AS db_size_mb FROM information_schema.TABLES WHERE table_schema = DATABASE() GROUP BY table_schema ;";
    $query = $this->db->query($sql);
    if ($query->num_rows() > 0) {
        
       $row = $query->row();
       $size = $row->db_size_mb;
       return $size;
            }
            else
            {
              return false;
            }        
}

相关问题