当前位置:首页 > PHP教程 > php应用 > 列表

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

发布:smiling 来源: PHP粉丝网  添加日期:2021-05-29 14:37:00 浏览: 评论:0 

这篇文章主要介绍了php计算整个mysql数据库大小的方法,涉及php操作MySQL数据库的相关技巧,需要的朋友可以参考下

本文实例讲述了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);

Tags: mysql数据库大小

分享到: