What are common methods to determine the size of a MySQL database in PHP?
To determine the size of a MySQL database in PHP, you can use the following methods: 1. Using the MySQL query "SHOW TABLE STATUS" to get the size of each table in the database. 2. Summing up the sizes of all tables to get the total database size.
<?php
// Connect to MySQL database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";
$conn = new mysqli($servername, $username, $password, $dbname);
// Query to get the size of each table
$sql = "SHOW TABLE STATUS";
$result = $conn->query($sql);
$totalSize = 0;
// Loop through each table and sum up the sizes
while($row = $result->fetch_assoc()) {
$totalSize += $row['Data_length'] + $row['Index_length'];
}
echo "Total database size: " . $totalSize . " bytes";
// Close the connection
$conn->close();
?>