johnmahugu

php - calculate full database sixe

Jun 14th, 2015
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.99 KB | None | 0 0
  1.  
  2.  
  3. function CalcFullDatabaseSize($database, $db) {
  4.  
  5.     $tables = mysql_list_tables($database, $db);
  6.     if (!$tables) { return -1; }
  7.  
  8.     $table_count = mysql_num_rows($tables);
  9.     $size = 0;
  10.  
  11.     for ($i=0; $i < $table_count; $i++) {
  12.         $tname = mysql_tablename($tables, $i);
  13.         $r = mysql_query("SHOW TABLE STATUS FROM ".$database." LIKE '".$tname."'");
  14.         $data = mysql_fetch_array($r);
  15.         $size += ($data['Index_length'] + $data['Data_length']);
  16.     };
  17.  
  18.     $units = array(' B', ' KB', ' MB', ' GB', ' TB');
  19.     for ($i = 0; $size > 1024; $i++) { $size /= 1024; }
  20.     return round($size, 2).$units[$i];
  21. }
  22.  
  23.  
  24. /*
  25. ** Example:
  26. */
  27.  
  28. // open mysql connection:
  29. $handle = mysql_connect('localhost', 'user', 'password');
  30.  
  31. if (!$handle) { die('Connection failed!'); }
  32.  
  33. // get the size of all tables in this database:
  34. print CalcFullDatabaseSize('customer1234', $handle);
  35. // --> returns something like: 484.2 KB
  36.  
  37. // close connection:
  38. mysql_close($handle);
Advertisement
Add Comment
Please, Sign In to add comment