PHP can obtain and generate a database dictionary. php can obtain a database dictionary.

Source: Internet
Author: User
Tags mysql connect

PHP can obtain and generate a database dictionary. php can obtain a database dictionary.

This example describes how PHP can obtain and generate a database dictionary. 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'], $ mysq Rochelle conn); $ result = mysql_query ('show tables ', $ mysql_conn); mysql_query ("set names utf8 "); // obtain 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. = '<table border = "1" cellspacing = "0" cellpadding = "0" align = "center">'; $ html. = '<caption> table name :'. $ v ['table _ name']. ''. $ v ['table _ comment']. '</caption>'; $ html. = '<tbody> <tr> <th> Field name </th> <th> data type </th> <th> default value </th> <th>> Allow non-null </th> <th> auto increment </th> <th> remarks </th> </tr> '; $ html. = ''; foreach ($ v ['column '] AS $ f) {$ html. = '<td class = "c1"> '. $ f ['column _ name']. '</td>'; $ html. = '<td class = "c2"> '. $ f ['column _ type']. '</td>'; $ html. = '<td class = "c3"> '. $ f ['column _ default']. '</td>'; $ html. = '<td class = "c4"> '. $ f ['is _ nulllable']. '</td>'; $ html. = '<td class = "c5"> '. ($ f ['extra '] = 'Auto _ secret '? 'Yes ':''). '</td>'; $ html. = '<td class = "c6"> '. $ f ['column _ comment']. '</td>'; $ html. = '</tr>';} $ html. = '</tbody> </table> </p>';}/* 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 '

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.