PHP methods for obtaining and generating database dictionaries

Source: Internet
Author: User
Tags mysql connect
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. ='
  
  '; $ Html. ='
  
  '; $ Html. = ''; foreach ($ v ['column'] AS $ f) {$ html. =' 
   '; $ Html. =' 
    '; $ Html. =' 
    '; $ Html. =' 
    '; $ Html. =' 
    '; $ Html. =' 
    '; $ Html. =' 
   ';} $ Html. =' 
  
Table name: '. $ v ['Table _ name'].'. $ v ['Table _ comment'].'
Field name Data type Default value Allow non-empty Auto increment Remarks
'. $ F ['column _ name'].''. $ F ['column _ type'].''. $ F ['column _ default'].''. $ F ['is _ nulllable'].''. ($ F ['Extra'] = 'auto _ secret '? 'Yes ':'').''. $ F ['column _ comment'].'

';}/* 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'';?>

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.