PHP代码快速生成mysql数据库词典,平常创建数据库最好给每个字段带上注释 修改下配置文件即可 mysqlcidian.php $v){ $sql = 'SELECT * FROM '; $sql .= 'information_schema.TABLES '; $sql .= 'WHERE '; $sql .= "ta
修改下配置文件即可mysqlcidian.php
$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 .= '
';
$html .= '
表名:' . $v['TABLE_NAME'] . ' ' . $v['TABLE_COMMENT'] . '
';
$html .= '
字段名
数据类型
默认值
允许非空
自动递增
备注
'; $html .= ''; foreach($v['COLUMN'] AS $f) { $html .= '
' . $f['COLUMN_NAME'] . ''; $html .= '
' . $f['COLUMN_TYPE'] . ''; $html .= '
' . $f['COLUMN_DEFAULT'] . ''; $html .= '
' . $f['IS_NULLABLE'] . ''; $html .= '
' . ($f['EXTRA'] == 'auto_increment'?'是':' ') . ''; $html .= '
' . $f['COLUMN_COMMENT'] . ''; $html .= '
'; } $html .= '
';
}
/* 生成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 '
自动生成数据字典
';
echo ''.$database['DB_NAME'].'数据字典
';
echo '生成时间:' . date('Y-m-d H:i:s') . '
';
echo $html;
echo '总共:' . count($tables) . '个数据表
';
echo '';
?>
TIM图片20171014162513.png
