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 = <