Please wait...
Popular Resources
Recent Forum Posts

Get MySQL Database size

Posted: 05-12-11 07:46     

If you are creating statistics for your website, then your database size might be a good one to have. Showing the size of your MySQL database with PHP is easy with MySQLs built in Query. elow is the function to do so.

Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 
<?php
 
function getDBSize()
{
    $query = mysql_query( “SHOW TABLE STATUS” );
    $size = 0; 
 
    while( $row = mysql_fetch_array( $query ) ) {  
        $size += $row["Data_length"]+$row["Index_length"];
    }
    return number_format( (($size*1024)*1024), 2, '.', '' ) . "MB";
}
 
?>
 
 

The function above assumes you already have an open connection to your database. To use this function just call the getDBSize function and it will output something similar to: 34.56MB.

Enjoy!