php計算整個mysql數(shù)據(jù)庫大小的方法
更新時間:2015年06月19日 15:51:04 作者:企鵝不笨
這篇文章主要介紹了php計算整個mysql數(shù)據(jù)庫大小的方法,涉及php操作MySQL數(shù)據(jù)庫的相關(guān)技巧,需要的朋友可以參考下
本文實例講述了php計算整個mysql數(shù)據(jù)庫大小的方法。分享給大家供大家參考。具體如下:
這里用MB,KB或者GB的格式返回計算結(jié)果。
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程序設(shè)計有所幫助。
相關(guān)文章
php刪除字符串末尾子字符,刪除開始字符,刪除兩端字符(實現(xiàn)代碼)
2013-06-06
PHP數(shù)組操作匯總 php數(shù)組的使用技巧
對于Web編程來說,最重要的就是存取和讀寫數(shù)據(jù)了。存儲方式可能有很多種,可以是字符串、數(shù)組、文件的形式等。2011-07-07
PHP+Ajax檢測用戶名或郵件注冊時是否已經(jīng)存在實例教程
這篇文章主要介紹了PHP+Ajax檢測用戶名或郵件注冊時是否已經(jīng)存在實例教程,是非常常見的一個重要功能,常用于論壇注冊部分或會員注冊部分,需要的朋友可以參考下2014-08-08

