runthis Posted April 7, 2013 Share Posted April 7, 2013 I thought I would post a function for you guys as I remembered I used to frequent here. This function will get the total size of your mysql database for you and present it in a readable format. This is nice for your admin back-end I will provide the function, followed by simple usage example function dbsize($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]; } Simple Usage $link=mysql_connect("localhost", "User", "Password"); mysql_select_db("Your_Database_Name",$link); echo "Database Size: ".dbsize('Your_Database_Name', $link); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.