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

yipeiwu_com6年前Mysql基础

本文实例讲述了php计算整个mysql数据库大小的方法。分享给大家供大家参考。具体如下:

这里用MB,KB或者GB的格式返回计算结果。

function CalcFullDatabaseSize($database, $db) {
  $tables = mysql_list_tables($database, $db);
  if (!$tables) { return -1; }
  $table_count = mysql_num_rows($tables);
  $size = 0;
  for ($i=0; $i < $table_count; $i++) {
    $tname = mysql_tablename($tables, $i);
    $r = mysql_query("SHOW TABLE STATUS FROM ".$database." LIKE '".$tname."'");
    $data = mysql_fetch_array($r);
    $size += ($data['Index_length'] + $data['Data_length']);
  };
  $units = array(' B', ' KB', ' MB', ' GB', ' TB');
  for ($i = 0; $size > 1024; $i++) { $size /= 1024; }
  return round($size, 2).$units[$i];
}
/*
** Example:
*/
// open mysql connection:
$handle = mysql_connect('localhost', 'user', 'password'); 
if (!$handle) { die('Connection failed!'); }
// get the size of all tables in this database:
print CalcFullDatabaseSize('customer1234', $handle);
// --> returns something like: 484.2 KB
// close connection:
mysql_close($handle);

希望本文所述对大家的php程序设计有所帮助。

相关文章

php数据入库前清理 注意php intval与mysql的int取值范围不同

php保存数据到mysql 打算在dao层进行数据入库前的清理,比如varchar进行trim,int进行intval。 有一天突然想起,php intval的取值范围与mysql的in...

PHP中mysql_field_type()函数用法

本文实例讲述了PHP中mysql_field_type()函数用法。分享给大家供大家参考。具体如下: 定义和用法:mysql_field_type() 函数返回结果集中指定字段的类型,如...

php读取mysql中文数据出现乱码的解决方法

1.PHP页面语言本身的编码类型不合适,这时候,你直接在脚本中写的中文肯定是乱码,不用说数据库了解决方法:选择'UTF8'或者'gb2312',这样客户浏览器会自动选择并出现正确的中文显...

PHP提示Deprecated: mysql_connect(): The mysql extension is deprecated的解决方法

本文实例讲述了PHP提示 Deprecated: mysql_connect(): The mysql extension is deprecated的解决方法,在PHP程序开发中常会遇...

PHP+MySQL高并发加锁事务处理问题解决方法

本文实例讲述了PHP+MySQL高并发加锁事务处理问题解决方法。分享给大家供大家参考,具体如下: 1、背景: 现在有这样的需求,插入数据时,判断test表有无username为‘mraz...