How to operate MySQL database with php

墨辰丷
Release: 2023-03-31 10:36:02
Original
2514 people have browsed it

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 &#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

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!

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!