PHP实现获取并生成数据库字典的方法
程序员文章站
2024-04-02 11:01:04
本文实例讲述了php实现获取并生成数据库字典的方法。分享给大家供大家参考,具体如下:
本文实例讲述了php实现获取并生成数据库字典的方法。分享给大家供大家参考,具体如下:
<?php /** * 生成mysql数据字典 */ header("content-type:text/html;charset=utf-8"); // 配置数据库 $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"); // 取得所有表名 while ($row = mysql_fetch_array($result)) { $tables[]['table_name'] = $row[0]; } // 循环取得所有表的备注及表中列消息 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 = ''; // 循环所有表 //print_r($tables); foreach($tables as $k => $v) { $html .= '<table border="1" cellspacing="0" cellpadding="0" align="center">'; $html .= '<caption>表名:' . $v['table_name'] . ' ' . $v['table_comment'] . '</caption>'; $html .= '<tbody><tr><th>字段名</th><th>数据类型</th><th>默认值</th><th>允许非空</th><th>自动递增</th><th>备注</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_nullable'] . '</td>'; $html .= '<td class="c5">' . ($f['extra'] == 'auto_increment'?'是':' ') . '</td>'; $html .= '<td class="c6">' . $f['column_comment'] . '</td>'; $html .= '</tr>'; } $html .= '</tbody></table></p>'; } /* 生成word */ //header ( "content-type:application/vnd.ms-word" ); //header ( "content-disposition:attachment;filename={$database['db_name']}数据字典.doc" ); /* 生成excel*/ //header ( "content-type:application/vnd.ms-excel" ); //header ( "content-disposition:attachment;filename={$database['db_name']}数据字典.xls" ); // 输出 echo '<html> <meta charset="utf-8"> <title>自动生成数据字典</title> <style> body,td,th {font-family:"宋体"; font-size:12px;} table,h1,p{width:960px;margin:0px auto;} table{border-collapse:collapse;border:1px solid #ccc;background:#efefef;} 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:26px; font-size:12px; border:1px solid #ccc;padding-left:5px;} table td{height:20px; font-size:12px; border:1px solid #ccc;background-color:#fff;padding-left:5px;} .c1{ width: 150px;} .c2{ width: 150px;} .c3{ width: 80px;} .c4{ width: 100px;} .c5{ width: 100px;} .c6{ width: 300px;} </style> <body>'; echo '<h1 style="text-align:center;">'.$database['db_name'].'数据字典</h1>'; echo '<p style="text-align:center;margin:20px auto;">生成时间:' . date('y-m-d h:i:s') . '</p>'; echo $html; echo '<p style="text-align:left;margin:20px auto;">总共:' . count($tables) . '个数据表</p>'; echo '</body></html>'; ?>
更多关于php相关内容感兴趣的读者可查看本站专题:《php基于pdo操作数据库技巧总结》、《php运算与运算符用法总结》、《php网络编程技巧总结》、《php基本语法入门教程》、《php操作office文档技巧总结(包括word,excel,access,ppt)》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家php程序设计有所帮助。