Determine size of SQL Database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
$var
Forum Contributor
Posts: 317
Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto

Determine size of SQL Database

Post by $var »

Code: Select all

<html><head><title>MySQL Database Size</title></head><body> 
<h1>MySQL Database Size</h1> 
<?php  
function file_size_info($filesize) { 
 $bytes = array('KB', 'KB', 'MB', 'GB', 'TB'); # values are always displayed  
 if ($filesize < 1024) $filesize = 1; # in at least kilobytes. 
 for ($i = 0; $filesize > 1024; $i++) $filesize /= 1024; 
 $file_size_info['size'] = ceil($filesize); 
 $file_size_info['type'] = $bytes[$i]; 
 return $file_size_info; 
} 
$db_server = 'localhost'; 
$db_user = 'userid'; 
$db_pwd = 'password'; 
$db_name = 'databasename'; 
$db_link = @mysql_connect($db_server, $db_user, $db_pwd) 
 or exit('Could not connect: ' . mysql_error()); 
$db = @mysql_select_db($db_name, $db_link) 
 or exit('Could not select database: ' . mysql_error()); 
// Calculate DB size by adding table size + index size: 
$rows = mysql_query("SHOW TABLE STATUS"); 
$dbSize = 0; 
while ($row = mysql_fetch_array($rows)) { 
 $dbSize += $row['Data_length'] + $row['Index_length']; 
} 
print "Database size is: $dbSize bytes<br />"; 
print 'or<br />'; 
$dbSize = file_size_info($dbSize); 
print "Database size is: {$dbSize['size']} {$dbSize['type']}"; 
?> 
</body></html>
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Same as mine here

viewtopic.php?t=21565&highlight=

But mine does total DB size, and size of individual tables too
Post Reply