当前位置:Gxlcms > PHP教程 > php计算整个mysql数据库的大小

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

时间:2021-07-01 10:21:17 帮助过:24人阅读

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 < $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. }
  16. /*
  17. ** Example:
  18. */
  19. // open mysql connection:
  20. $handle = mysql_connect('localhost', 'user', 'password');
  21. if (!$handle) { die('Connection failed!'); }
  22. // get the size of all tables in this database:
  23. print CalcFullDatabaseSize('customer1234', $handle);
  24. // --> returns something like: 484.2 KB
  25. // close connection:
  26. mysql_close($handle);

php, mysql

人气教程排行