Home >Backend Development >PHP Tutorial >PHP code sharing for calculating the size of mysql database
This article mainly shares with you the method of calculating the size of the entire mysql database in PHP. Here, the calculation results are returned in the format of MB, KB or GB. Hope it helps everyone.
function CalcFullDatabaseSize($database, $db) { $tables = mysql_list_tables($database, $db); if (!$tables) { return -1; } $table_count = mysql_num_rows($tables); $size = 0; for ($i=0; $i < $table_count; $i++) { $tname = mysql_tablename($tables, $i); $r = mysql_query("SHOW TABLE STATUS FROM ".$database." LIKE '".$tname."'"); $data = mysql_fetch_array($r); $size += ($data['Index_length'] + $data['Data_length']); }; $units = array(' B', ' KB', ' MB', ' GB', ' TB'); for ($i = 0; $size > 1024; $i++) { $size /= 1024; } return round($size, 2).$units[$i]; } /* ** Example: */ // open mysql connection: $handle = mysql_connect('localhost', 'user', 'password'); if (!$handle) { die('Connection failed!'); } // get the size of all tables in this database: print CalcFullDatabaseSize('customer1234', $handle); // --> returns something like: 484.2 KB // close connection: mysql_close($handle);
Related recommendations:
mysql delete The database size remains unchanged after deleting the record_MySQL
php method to calculate the size of the entire mysql database, php calculates the mysql database
The above is the detailed content of PHP code sharing for calculating the size of mysql database. For more information, please follow other related articles on the PHP Chinese website!