The size of the database. PHP - MySQL

I want to know the size of my database using php. How to display the size in megabytes of the entire database? The size in megabytes of a specific request?

+5
source share
4 answers

Try this to get the size in bytes:

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"];  
}

then convert to megabytes:

$decimals = 2;  
$mbytes = number_format($size/(1024*1024),$decimals);
+9
source
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 ;
+6
source

I am trying to change from an internet source for this case, just try.

<?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'];
+2
source

Try to request information_schema.

0
source

All Articles