Jump to content

Get Size of Mysql Database


Recommended Posts

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");

echo "Database Size: ".dbsize('Your_Database_Name', $link);
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...