PHP generates MySQL data dictionary code

Source: Internet
Author: User
Tags mysql connect

Because the project development uses the more table, in order to obtain the data dictionary quickly, obtains the table structure and the table annotation through the PHP code way. The code is as follows:

<?PHP/** * Generate MySQL data dictionary*/Header("content-type:text/html; Charset=utf-8 " );//Configuration Database$dbserver= "localhost";$dbusername= "Database user name";$dbpassword= "Database Password";$database= "Database Name";//Other Configurations$title= ' Data dictionary ';$mysql _conn= @mysql_connect( "$dbserver", "$dbusername", "$dbpassword") or die("Mysql Connect is error.") );mysql_select_db($database,$mysql _conn );mysql_query(' SET NAMES UTF8 ',$mysql _conn );$table _result=mysql_query(' Show Tables ',$mysql _conn );//get all the table names while($row=Mysql_fetch_array($table _result ) ) {    $tables[] [' table_name '] =$row[0];}//Loop to get notes for all tables and column messages in the tableforeach($tables  as $k=$v ) {    $sql= ' SELECT * from '; $sql. = ' Information_schema. TABLES '; $sql. = ' WHERE '; $sql. = "table_name = ' {$v[' table_name ']} ' and Table_schema = ' {$database}‘"; $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}‘"; $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 Tablesforeach($tables  as $k=$v ) {    //$html. = ' <p>    $html. = ' <table border= ' 1 "cellspacing=" 0 "cellpadding=" 0 "align=" center "> '; $html. = ' <caption> '.$v[' table_name ']. ‘ ‘ .$v[' Table_comment ']. ' </caption> '; $html=<tbody><tr><th> Field name </th><th> data type </th><th> default </th> <th> Allow non-empty </th> <th> auto increment </th><th> remarks </th></tr>‘; $html. = '; foreach($v[' COLUMN '] as $f ) {        $html. = ' <tr><td class= ' C1 ' > '.$f[' column_name ']. ' </td> '; $html. = ' <td class= ' C2 ' > '.$f[' Column_type ']. ' </td> '; $html. = ' <td class= ' C3 ' >&nbsp; '.$f[' Column_default ']. ' </td> '; $html. = ' <td class= ' C4 ' >&nbsp; '.$f[' is_nullable ']. ' </td> '; $html. = ' &LT;TD class= ' c5 ' > '. ($f[' EXTRA '] = = ' auto_increment '? ' Yes ': ' &nbsp; ') . ' </td> '; $html. = ' <td class= ' c6 ' >&nbsp; '.$f[' Column_comment ']. ' </td> '; $html. = ' </tr> '; }    $html. = ' </tbody></table></p> ';}//OutputEcho‘‘ .$title. ‘</title><style>body,td,th {font-family: "Song body"; font-size:12px;} table{border-collapse:collapse;border:1px solid #CCC; background: #6089D4;} Table caption{text-align:left; Background-color: #fff; line-height:2em; font-size:14px; font-weight:bold;} Table th{text-align:left; font-weight:bold;height:26px; line-height:25px; font-size:16px; border:3px solid #fff; color : #ffffff; padding:5px;} Table td{height:25px; font-size:12px; border:3px solid #fff; Background-color: #f0f0f0; padding:5px;}. c1{width:150px;}. c2{width:130px;}. c3{width:70px;}. c4{width:80px;}. c5{width:80px;}. c6{width:300px;} </style>‘;Echo' 

$title. ' ;Echo $html;Echo' </body>;?>

PHP generates MySQL data dictionary code

Related Article

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.