得到的结果是以字节为单位,除1024为K,除1048576为M。
1.
public function getDBSize($dbName,$tblPrefix=null){
$sql = "SHOW TABLE STATUS FROM ".$dbName;
if($tblPrefix != null){
$sql .= "LIKE '$tblPrefix%'";
}
$result = $this->mysqli->query($sql);
$size = 0;
while (($row = $result->fetch_assoc())!=false){
$size += $row["Data_length"]+$row["Index_length"];
}
return $size;
}
2.
public function getDBSize2($dbName){
$sql = "SELECT sum(DATA_LENGTH)+sum(INDEX_LENGTH) as db_size
FROM information_schema.TABLES where TABLE_SCHEMA='$dbName'";
$result = $this->mysqli->query($sql);
$size = 0;
if (($row = $result->fetch_assoc())!=false){
$size = $row["db_size"];
}
return $size;
}