Results 1 to 3 of 3

Thread: Get database size?

  1. #1
    Join Date
    Oct 2007
    Posts
    53
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default Get database size?

    Hey I want to get the size of all of the information in a database, I have this code:
    PHP Code:
    <?php

    $db 
    mysql_connect("server""username","password");  // Connecting to the server.
    mysql_select_db("database",$db); // Specify the database.

    function mysql_getDatabaseUsage()
    {
        
    $result mysql_query("SHOW TABLE STATUS"); // Get the table status...
        
    while($row mysql_fetch_array($result)) // Get each row
        
    {
            
    $total $row['Data_length'] + $row['Index_length']; // Add Data_length and Index_length
        
    }
        return(
    $total); // Return the result in bytes.
    }

    echo(
    mysql_getDatabaseUsage()); // Echo the amount of the database used that we are currently connected to in bytes.

    mysql_close($db); // Close database connection
    ?>
    That, however, generates this error:

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in */mysql.php on line 10

    * The long directory to the file...

    Any help would be well appreciated!
    Last edited by AmenKa; 11-11-2008 at 09:19 AM. Reason: Resolution reached

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    on this line:

    Code:
    $result = mysql_query("SHOW TABLE STATUS");
    put the following:

    Code:
    $result = mysql_query("SHOW TABLE STATUS") or die('MySQL Error: '.mysql_error());
    then post back if you get an error. I think what is happening is that the database connection is not being passed to the function and throwing an error, but I could be wrong.

    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  3. The Following User Says Thank You to thetestingsite For This Useful Post:

    AmenKa (11-11-2008)

  4. #3
    Join Date
    Oct 2007
    Posts
    53
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default

    Nice call! I should have tested for a MySQL error myself!

    MySQL Error: No database selected

    Ok so I adjusted it to properly select the database (entered wrong db)...

    Had some issues but I modified it some more to work... I didn't initialize the total to 0 and add each row to it lol... final function:

    PHP Code:
    function mysql_getDatabaseUsage()
    // Calculates the amount of data in 
        
    $sql "SHOW TABLE STATUS";
        
    $result mysql_query($sql) or die('MySQL Error: '.mysql_error()); // This is the result of executing the query
        
    $total 0// Initialize $total to 0.
        
    while($row mysql_fetch_array($result))// Here we are to add the columns 'Index_length' and 'Data_length' of each row
        
    {
            
    $total += $row['Data_length'] + $row['Index_length'];
        }
        
    $total /= 1024// Convert from Bytes to KBytes.
        
    $total /= 1024// KBytes to MBytes
        
    $total round($total1); // Round the total.
        
    return($total); // here we print the file size in bytes

    Last edited by AmenKa; 11-11-2008 at 09:18 AM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •