Home > Article > Backend Development > How to operate MySQL database with php
This article mainly introduces the method of operating MySQL database in PHP. Interested friends can refer to it. I hope it will be helpful to everyone.
The example in this article describes how PHP calculates the size of the entire mysql database. The details are as follows:
Here the calculation results are returned in the format of MB, KB or GB.
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 '".$tname."'"); $data = mysql_fetch_array($r); $size += ($data['Index_length'] + $data['Data_length']); }; $units = array(' B', ' KB', ' MB', ' GB', ' TB'); for ($i = 0; $size > 1024; $i++) { $size /= 1024; } return round($size, 2).$units[$i]; } /* ** Example: */ // open mysql connection: $handle = mysql_connect('localhost', 'user', 'password'); if (!$handle) { die('Connection failed!'); } // get the size of all tables in this database: print CalcFullDatabaseSize('customer1234', $handle); // --> returns something like: 484.2 KB // close connection: mysql_close($handle);
Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
php to implement news release system
PHP reading configuration file class example
php function to query songs through the Sina music library search interface
The above is the detailed content of How to operate MySQL database with php. For more information, please follow other related articles on the PHP Chinese website!