Php+mysql a method that counts the number of records in each table in the library and is sorted in descending order _php tips

Source: Internet
Author: User
Tags dsn stmt

This example describes a method that Php+mysql counts the number of records in each table in the library and in descending order. Share to everyone for your reference, specific as follows:

This is a simple piece of code that enables you to count the number of records in each table in the database, and to sort the functionality in descending order

$host = ' 127.0.0.1 ';
$port = 3306;
$dbname = ' Test ';
$username = ' root ';
$password = ';
  function ee ($p) {if (Php_sapi = = ' cli ') {echo "\ n";
  }else{echo "<pre>";
  } print_r ($p);
  if (Php_sapi = = ' cli ') {echo ' \ n ';
  }else{echo "<pre>";
} $DSN = "mysql:host={$host};p ort={$port};d bname={$dbname}";
$opts = Array (pdo::attr_errmode=>pdo::errmode_exception, pdo::attr_autocommit=>0); try {$pdo = new PDO ($DSN, $username, $password, $opts);
  catch (Pdoexception $e) {echo $e->getmessage ();}//has query Results function query ($sql) {global $pdo;
  $stmt = $pdo->query ($sql);
  $data = $stmt->fetchall (pdo::fetch_both);
return $data;
  //No query result function execute ($sql) {global $pdo;
  $affect _rows = $pdo->query ($sql);
return $affect _rows;//Impact Bar} $tables = Query ("Show Tables");
$sort _data = Array ();
  foreach ($tables as $table) {//table record Bar number $count _sql = "SELECT count (*) as num from {$table [0]}";
  $stmt = $pdo->query ($count _sql); $info = $stmt->fetch (pdo::fetch_both);
  $pad _table = str_pad ($table [0], 25, ' ");
  $sort _data[] = array (' table ' => $pad _table, ' num ' => $info [' num ']);
$sort _index[] = $info [' num '];
Array_multisort ($sort _index, Sort_desc, $sort _data);
 foreach ($sort _data as $val) {$row _str = <<<eof {$val [' table ']} [{$val [' num ']}] EOF;
EE ($row _str);

 }

For more information about PHP interested readers can view the site topics: "Php+mysql Database Operation Introduction Tutorial", "PHP common database Operation skills Summary", "PHP based on PDO Operation Database skills summary" and "PHP Basic grammar Introductory Tutorial"

I hope this article will help you with the PHP program design.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.