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 "<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 ();} 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 = <<<eof {$val [' Table ']} [{$val [' num ']}]eof; EE ($row _str);}
I hope this article is helpful to you in PHP programming.