





This simple tutorial will show you how to get the size of a MySQL database using PHP in just a few simple steps.
First of you'll need my formatfilesize() function; for this script to work you will obviously need to connect to your MySQL database. Then add this line of code but replace "databasename" with the name of your database:
<?php
$dbname = "databasename";
?>
Now comes the fundamental part of the tutorial: computing the database size. It's actually very simple - all we need to do is to go through each table in the database and add up the length of the data and the index:
We now have the size of the databse in bytes sotred in the $dbsize variable. Now we just output it to a more user-friendly format using my formatfilesize() function:
<?php
echo "<p>The size of the database is " . formatfilesize( $dbsize ) . "</p>";
?>
That's about the size of it!
These resources are provided free of charge. If, however, you would like to make a donation to help finance these resources, no matter how big or small, please use the PayPal button below.
This resource was written by Tim Bennett Bsc (Hons). Tim Bennett has a First Class Degree in Multimedia Systems Computing from Leeds Metropolitan University. He works as a freelance multimedia developer under the company name Texelate creating free and commissioned high-quality multimedia solutions. Find out more by visiting the portfolio section and viewing his blog. If you'd like to request a resource get in touch!







