Calculate full database size


SUBMITTED BY: phpsnippets

DATE: Oct. 21, 2015, 2:11 p.m.

FORMAT: Text only

SIZE: 657 Bytes

HITS: 1835

  1. function CalcFullDatabaseSize($database, $db) {
  2. $tables = mysql_list_tables($database, $db);
  3. if (!$tables) { return -1; }
  4. $table_count = mysql_num_rows($tables);
  5. $size = 0;
  6. for ($i=0; $i < $table_count; $i++) {
  7. $tname = mysql_tablename($tables, $i);
  8. $r = mysql_query("SHOW TABLE STATUS FROM ".$database." LIKE '".$tname."'");
  9. $data = mysql_fetch_array($r);
  10. $size += ($data['Index_length'] + $data['Data_length']);
  11. };
  12. $units = array(' B', ' KB', ' MB', ' GB', ' TB');
  13. for ($i = 0; $size > 1024; $i++) { $size /= 1024; }
  14. return round($size, 2).$units[$i];
  15. }

comments powered by Disqus