Q:

How to get mySQL Database max. capacity in php?

Is there a way to read the max. capacity of a MySQL database in PHP?
I have made a class to secure the databases from gettin overflowed. It works great, only problem is i cant get the max DB size (of each db). I have to set it manually like this:
class StorageSecurity
{
    private static $MAX_DB_SIZE = 100; //MB

    private $_db_host;  //Database Host
    private $_db_user;  //Database Username
    private $_db_pass;  //Database Userpass

    public function  __construct(Database $_db)
    {
      try
      {
        $this->_errors  = Array();
        $this->_db_host = $_db->getDBHost();
        $this->_db_user = $_db->getDBUser();
        $this->_db_pass = $_db->getDBPass();

        $this->setMaxDBCapacity(self::$MAX_DB_SIZE);

        $db_conn = mysql_pconnect($this->_db_host, $this->_db_user,
                                  $this->_db_pass);

        if (!$db_conn) throw new Exception("Server Connection Failed.");

      } catch(Exception $e) {
        $this->_errors[] = $e->getMessage();
      }
   }
}

Thanks!

A:

Assuming by "max db size" you mean "maximum size of the associated db files such that it will not exceed the available storage space", it obviously depends on whether or not other applications can write to the same filesystem partition as the database.
In MySQL, you can use INFORMATION_SCHEMA.tables to calculate the estimate (from the storage engine, e.g. InnoDB) of the amount of disk space used by a database (test in this case):
SELECT SUM(DATA_LENGTH) + SUM(INDEX_LENGTH) AS usage_estimate 
FROM INFORMATION_SCHEMA.tables WHERE table_schema = 'test';

You can also select the MySQL limit (but not the filesystem limit) per table:
SELECT TABLE_NAME, MAX_DATA_LENGTH 
FROM INFORMATION_SCHEMA.tables WHERE table_schema = 'test';

You could then compare this to some configurable value in your code that you considered the "safe" size for each database (keeping in mind that log files, etc. will add to the overall "size" of the database on disk).
More information about INFORMATION_SCHEMA tables can be found here: http://dev.mysql.com/doc/refman/5.1/en/information-schema.html

