我想用php知道我的数据库大小。如何显示整个数据库以兆字节为单位的大小?还可以查询特定请求的大小吗?
尝试使用以下代码获取以字节为单位的大小:
mysql_select_db("yourdatabase");
$q = mysql_query("SHOW TABLE STATUS");
$size = 0;
while($row = mysql_fetch_array($q)) {
$size += $row["Data_length"] + $row["Index_length"];
}
然后转换为兆字节:
$decimals = 2;
$mbytes = number_format($size/(1024*1024),$decimals);
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 ;
我尝试从互联网上获取相关资源进行修改,请尝试一下。
<?php
mysql_connect("localhost","root","password here");
$namadb=''; //put db name here
$sql="SELECT table_schema 'db_name', SUM( data_length + index_length) / 1024 / 1024 'db_size_in_mb' FROM information_schema.TABLES WHERE table_schema='$namadb' GROUP BY table_schema ;";
$query=mysql_query($sql);
$data=mysql_fetch_array($query);
print $data['db_size_in_mb'];
SELECT
TABLE_SCHEMA AS DB_Name,
count(TABLE_SCHEMA) AS Total_Tables,
SUM(TABLE_ROWS) AS Total_Tables_Row,
ROUND(sum(data_length + index_length)/1024/1024) AS "DB Size (MB)",
ROUND(sum( data_free )/ 1024 / 1024) AS "Free Space (MB)"
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = 'YOUR_DATABASE_NAME'
GROUP BY TABLE_SCHEMA ;
尝试查询 information_schema
。