//以下一段内容来自phpMyAdmin的查询每个数据库大小的程序$dbs = mysql_list_dbs() ;while ($a_db = mysql_fetch_object($dbs)) { //查询数据名,以后一段代码来自phpmyadmin $dblist[] = $a_db->Database;} // end whilemysql_free_result($dbs);$num_dbs = count($dblist); $total_array[0] = 0; // number of tables $total_array[1] = 0; // total data size $total_array[2] = 0; // total index size $total_array[3] = 0; // big total size
// Gets the tables stats per database for ($i = 0; $i < $num_dbs; $i++) { $db = $dblist[$i]; $tables = mysql_list_tables($db);
// Number of tables if ($tables) { $dbs_array[$db][0] = mysql_numrows($tables); mysql_free_result($tables); } else { $dbs_array[$db][0] = 0; } $total_array[0] += $dbs_array[$db][0];
// Size of data and indexes $dbs_array[$db][1] = 0; // data size column $dbs_array[$db][2] = 0; // index size column $dbs_array[$db][3] = 0; // full size column
$local_query = 'SHOW TABLE STATUS FROM ' . PMA_backquote($db); //echo $db; $result = @mysql_query($local_query); // needs the "@" below otherwise, warnings in case of special DB names if ($result ) { while ($row = mysql_fetch_array($result)) { $dbs_array[$db][1] += $row['Data_length']; $dbs_array[$db][2] += $row['Index_length']; } $dbs_array[$db][3] = $dbs_array[$db][1] + $dbs_array[$db][2]; $total_array[1] += $dbs_array[$db][1]; $total_array[2] += $dbs_array[$db][2]; $total_array[3] += $dbs_array[$db][3]; // echo $dbs_array[$db][3]."<br>"; mysql_free_result($result); } // end if} // end for//查询数据库完毕。 //以下代码用来判断数据库大小是否达到限制等。。。。mysql_select_db('db_limit',$id); //db_limit数据库名可自由更改。但需要与其它程序保持一致
for ($i = 0; $i < $num_dbs; $i++) { $db=$dblist[$i]; $query='select * from dbs where db_name="'.$db.'"'; $result=mysql_query($query); if ($result) { $dbs_limit=mysql_fetch_array($result); $limit_size=$dbs_limit['db_limit_size']; $user=$dbs_limit['db_user']; $limited=$dbs_limit['db_has_limit']; $used_size=$dbs_limit['db_size_used']; $email=$dbs_limit['db_email']; $warning_size=$limit_size*0.8; //0.8=80%,你可以更改为其它比例 if ($user!=''){ //这 if ($dbs_array[$db][3] > $limit_size) {limit($user,$db);$limited='1';} if ($dbs_array[$db][3] > $warning_size) warning($user,$email); $query='update dbs set db_has_limit="'.$limited.'" , db_size_used="'.$dbs_array[$db][3].'" where db_name="'.$db.'"'; $result=mysql_query($query); //更新数据库大小至数据资料库 } //echo $query;exit; }}mysql_close($id);?>