Php calculates the size of the entire mysql database
|
Php calculates the size of the connected mysql database, which is returned in MB, KB or GB format.
- 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 ('mermer1234', $ handle );
- // --> Returns something like: 484.2 KB
- // Close connection:
- Mysql_close ($ handle );
|
Php, mysql