PHP + MySQL: how to count the number of records in each table in the database and sort the records in descending order. mysql record count _ PHP Tutorial

Source: Internet
Author: User
Tags dsn
PHP + MySQL calculates the number of records for each table in the database and sorts the records in descending order. PHP + MySQL calculates the number of records for each table in the database and sorts the records in descending order, mysql record count this article describes how to count the number of records of each table in the database in PHP + MySQL and sort the records of each table in the database in descending order. arrangement method, mysql records

This example describes how to count the number of records of each table in the database in PHP + MySQL and sort the records in descending order. We will share this with you for your reference. The details are as follows:

This is a simple code that allows you to count the number of records for each table in the database and sort the records in descending order.

$ Host = '2017. 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 }; port ={$ port}; dbname ={$ 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 () ;}// 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; // number of affected items} $ tables = query ("show tables"); $ sort_data = array (); foreach ($ tables as $ table) {// number of table records $ 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 = <
   
    

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.