Heim  >  Artikel  >  Backend-Entwicklung  >  php计算整个mysql数据库的大小

php计算整个mysql数据库的大小

WBOY
WBOYOriginal
2016-07-25 08:45:26909Durchsuche

php计算连接的mysql数据库的大小,用MB,KB或者GB的格式返回

  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 $tname = mysql_tablename($tables, $i);
  7. $r = mysql_query("SHOW TABLE STATUS FROM ".$database." LIKE '".$tname."'");
  8. $data = mysql_fetch_array($r);
  9. $size += ($data['Index_length'] + $data['Data_length']);
  10. };
  11. $units = array(' B', ' KB', ' MB', ' GB', ' TB');
  12. for ($i = 0; $size > 1024; $i++) { $size /= 1024; }
  13. return round($size, 2).$units[$i];
  14. }
  15. /*
  16. ** Example:
  17. */
  18. // open mysql connection:
  19. $handle = mysql_connect('localhost', 'user', 'password');
  20. if (!$handle) { die('Connection failed!'); }
  21. // get the size of all tables in this database:
  22. print CalcFullDatabaseSize('customer1234', $handle);
  23. // --> returns something like: 484.2 KB
  24. // close connection:
  25. mysql_close($handle);
复制代码

php, mysql


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn