Php+mysql to count the number of records in each table in the library and to sort them in descending order

Source: Internet
Author: User
The example in this article describes the method by which Php+mysql counts the number of records in each table in the library and in descending order. Share to everyone for your reference, as follows:

This is a simple code that implements the ability to count the number of records in each table in the database, and to sort 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 "
";  } print_r ($p);  if (Php_sapi = = ' cli ') {echo "\ n"; }else{echo "
"; }} $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 ();}  Query result 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;//affects the number of bars} $tables = Query ("Show Tables"), $sort _data = Array (), foreach ($tables as $table) {//table record bars $coun  T_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 = <<
   
     
     

More readers interested in PHP related content can view this site topic: php+ MySQL database operation Getting Started tutorial, "PHP Common database Operation skills Summary", "PHP based on PDO Operation Database skills summary" and "PHP Basic grammar Introductory Tutorial"

Hope that this article on the PHP program to help you. The

Above describes the method by which Php+mysql counts the number of records in each table in the library in descending order, including the Content and want to be helpful to a friend who is interested in PHP tutorials.

  • 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.