VN:F [1.9.22_1171]
Rating: 7.0/10 (1 vote cast)

This is a simple tutorial that shows how to get the size of the mysql databse using php.

//connect to DB
$objConnect = mysql_connect("localhost","root","") or die("Error Connecting to Database, <hr />Known issues:Database may be down or need to contact [email protected]");
 function getdbsize( $data ) {  
        // bytes
        if( $data < 1024 ) {
           return $data . " bytes";
        }
        // kilobytes
        else if( $data < 1024000 ) {
            return round( ( $data / 1024 ), 2 ) . "KB";
        }
        // megabytes

        else {        
            return round( ( $data / 1024000 ), 2 ) . " MB";
        }
    }
   $dbname = 'mydatabase';
    mysql_select_db($dbname);
    $result = mysql_query("SHOW TABLE STATUS");
    $dbsize = 0;
    while( $row = mysql_fetch_array( $result ) ) {  
        $dbsize += $row&#91; "Data_length" &#93; + $row&#91; "Index_length" &#93;;
		}
echo "<p>The size of the database is ". getdbsize( $dbsize );
VN:F [1.9.22_1171]
Rating: 7.0/10 (1 vote cast)

Leave a Reply

Your email address will not be published. Required fields are marked *