This article mainly introduces how PHP can obtain and generate a database dictionary. it can read the database and list detailed database information, for more information about how to obtain and generate a database dictionary using PHP, see the following example. We will share this with you for your reference. The details are as follows:
<? Php/*** generate mysql data dictionary */header ("Content-type: text/html; charset = utf-8"); // configure database $ database = array (); $ database ['DB _ host'] = 'localhost'; $ database ['DB _ name'] = 'test '; $ database ['DB _ user'] = 'root'; $ database ['DB _ pwd'] = ''; $ mysql_conn = @ mysql_connect ("{$ database ['DB _ host']}", "{$ database ['DB _ user']}", "{$ database ['DB _ pwd']}") or die ("Mysql connect is error. "); mysql_select_db ($ database ['DB _ name'], $ mysql_conn); $ result = mysql_query ('Show tables ', $ mysql_conn ); mysql_query ("set names utf8"); // get all table names while ($ row = mysql_fetch_array ($ result )) {$ tables [] ['Table _ name'] = $ row [0];} // cyclically obtain the remarks of all tables and the foreach ($ tables as $ k => $ v) {$ SQL = 'select * from'; $ SQL. = 'information _ schema. TABLES '; $ SQL. = 'where'; $ SQL. = "table_name = '{$ v ['Table _ name']}' AND table_schema = '{$ database ['DB _ name']}'"; $ table_result = mysql_query ($ SQL, $ mysql_conn); while ($ t = mysql_fetch_array ($ table_result )) {$ tables [$ k] ['Table _ comment'] = $ t ['Table _ comment'];} $ SQL = 'select * from'; $ SQL. = 'information _ schema. COLUMNS '; $ SQL. = 'where'; $ SQL. = "table_name = '{$ v ['Table _ name']}' AND table_schema = '{$ database ['DB _ name']}'"; $ fields = array (); $ field_result = mysql_query ($ SQL, $ mysql_conn); while ($ t = mysql_fetch_array ($ field_result) {$ fields [] = $ t ;} $ tables [$ k] ['column'] = $ fields;} mysql_close ($ mysql_conn); $ html = ''; // loop all tables // print_r ($ tables); foreach ($ tables as $ k => $ v) {$ html. ='
'; $ Html. ='
Table name: '. $ v ['Table _ name'].'. $ v ['Table _ comment'].'
'; $ Html. ='
Field name |
Data type |
Default value |
Allow non-empty |
Auto increment |
Remarks |
'; $ Html. = ''; foreach ($ v ['column'] AS $ f) {$ html. ='
'. $ F ['column _ name'].' | '; $ Html. ='
'. $ F ['column _ type'].' | '; $ Html. ='
'. $ F ['column _ default'].' | '; $ Html. ='
'. $ F ['is _ nulllable'].' | '; $ Html. ='
'. ($ F ['Extra'] = 'auto _ secret '? 'Yes ':'').' | '; $ Html. ='
'. $ F ['column _ comment'].' | '; $ Html. ='
';} $ Html. ='
';}/* Generate word * // header ("Content-type: application/vnd. ms-word "); // header (" Content-Disposition: attachment?filename=**database='db_name'}. .doc ");/* generate an excel file * // header (" Content-type: application/vnd. ms-excel "); // header (" Content-Disposition: attachment?filename=$database='db_name'} .xls "); // output echo'
Automatically generate data dictionary '; Echo ''. $ database ['DB _ name']. 'data Dictionary'; echo'
Generation Time: '. date ('Y-m-d H: I: s ').'
'; Echo $ html; echo'
Total: '. count ($ tables).' data tables
'; Echo'';?>