PHP code sharing for calculating the size of mysql database

小云云
Release: 2023-03-20 20:54:02
Original
1690 people have browsed it

This article mainly shares with you the method of calculating the size of the entire mysql database in PHP. Here, the calculation results are returned in the format of MB, KB or GB. Hope it helps everyone.

function CalcFullDatabaseSize($database, $db) {
  $tables = mysql_list_tables($database, $db);
  if (!$tables) { return -1; }
  $table_count = mysql_num_rows($tables);
  $size = 0;
  for ($i=0; $i < $table_count; $i++) {
    $tname = mysql_tablename($tables, $i);
    $r = mysql_query("SHOW TABLE STATUS FROM ".$database." LIKE &#39;".$tname."&#39;");
    $data = mysql_fetch_array($r);
    $size += ($data[&#39;Index_length&#39;] + $data[&#39;Data_length&#39;]);
  };
  $units = array(&#39; B&#39;, &#39; KB&#39;, &#39; MB&#39;, &#39; GB&#39;, &#39; TB&#39;);
  for ($i = 0; $size > 1024; $i++) { $size /= 1024; }
  return round($size, 2).$units[$i];
}
/*
** Example:
*/
// open mysql connection:
$handle = mysql_connect(&#39;localhost&#39;, &#39;user&#39;, &#39;password&#39;);
if (!$handle) { die(&#39;Connection failed!&#39;); }
// get the size of all tables in this database:
print CalcFullDatabaseSize(&#39;customer1234&#39;, $handle);
// --> returns something like: 484.2 KB
// close connection:
mysql_close($handle);
Copy after login

Related recommendations:

php method to calculate the size of the entire mysql database, php calculation mysql database_PHP tutorial

mysql delete The database size remains unchanged after deleting the record_MySQL

php method to calculate the size of the entire mysql database, php calculates the mysql database

The above is the detailed content of PHP code sharing for calculating the size of mysql database. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!