Home > Backend Development > PHP Tutorial > PHP calculates the size of the entire mysql database

PHP calculates the size of the entire mysql database

WBOY
Release: 2016-07-25 08:45:26
Original
970 people have browsed it

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


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template