欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

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

程序员文章站 2022-05-16 23:27:40
...

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