您好,登錄后才能下訂單哦!
這篇文章主要介紹thinkphp5.0框架如何生成數據庫字典,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
示例代碼使用PHP框架:Thinkphp5.0
PHP代碼:
/** * 生成數據庫字典html * 可直接另存為再copy到word文檔中使用 * * @return mixed */ public function dataDictionary() { $tables = Db::query('SHOW TABLE STATUS'); $table_list = array_map('array_change_key_case', $tables); $table_data = []; foreach ($table_list as $item) { $table_name = str_replace(config('database.prefix'),'',$item['name']); $table_fields = $this->showColumns($table_name); foreach ($table_fields as &$fieldItem) { $fieldItem['comment'] = $this->getDbColumnComment($table_name, $fieldItem['name']); } $table_data[] = [ 'table_name' => $item['name'], 'table_comment' => $item['comment'], 'table_fields' => $table_fields ]; } $this->assign('table_data', $table_data); return $this->fetch(''); } /** * 顯示表結構信息 * * @param string $table * @return array */ private function showColumns($table){ $sql = 'SHOW COLUMNS FROM `'.config('database.prefix').$table.'`'; $result = Db::query($sql); if ($result === false) return array(); $array = array(); if (!empty($result)) { foreach ($result as $k=>$v) { $array[$v['Field']] = [ 'name' => $v['Field'], 'type' => $v['Type'], 'null' => $v['Null'], 'default' => $v['Default'], 'primary' => (strtolower($v['Key']) == 'pri'), 'autoinc' => (strtolower($v['Extra']) == 'auto_increment'), ]; } } return $array; } /** * 獲取數據庫字段注釋 * * @param string $table_name 數據表名稱(必須,不含前綴) * @param string|boolean $field 字段名稱(默認獲取全部字段,單個字段請輸入字段名稱) * @param string $table_schema 數據庫名稱(可選) * @return string */ private function getDbColumnComment($table_name = '', $field = true, $table_schema = ''){ // 接收參數 $database = config('database'); $table_schema = empty($table_schema) ? $database['database'] : $table_schema; $table_name = $database['prefix'] . $table_name; // 處理參數 $param = [ $table_name, $table_schema ]; // 字段 $columnName = ''; if($field !== true){ $param[] = $field; $columnName = "AND COLUMN_NAME = ?"; } // 查詢結果 $result = Db::query("SELECT COLUMN_NAME as field,column_comment as comment FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = ? AND table_schema = ? $columnName", $param); if(empty($result) && $field !== true){ return $table_name . '表' . $field . '字段不存在'; } // 處理結果 foreach($result as $k => $v){ $data[$v['field']] = $v['comment']; if(strpos($v['comment'], '#*#') !== false){ $tmpArr = explode('#*#', $v['comment']); $data[$v['field']] = json_decode(end($tmpArr), true); } } // 字段注釋格式不正確 if(empty($data)){ return $table_name . '表' . $field . '字段注釋格式不正確'; } return count($data) == 1 ? reset($data) : $data; }
Html代碼: 相關教程:HTML視頻教程
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>快速生成數據庫字典</title> <style type="text/css"> .table-name { text-align: center; margin: 15px auto; font-weight: bold; font-size: 20px; } table { border-collapse: collapse; margin: 0 auto; text-align: center; width: 550px; } table caption { margin: 15px auto; } table td, table th { border: 1px solid #cad9ea; color: #666; height: 30px; } table thead th { background-color: #CCE8EB; width: 100px; } table tr:nth-child(odd) { background: #fff; } table tr:nth-child(even) { background: #F5FAFA; } </style> </head> <body> {foreach $table_data as $tableInfo} <div class="table-name">{$tableInfo.table_name}(表注釋:{$tableInfo.table_comment})</div> <table class="table"> <thead> <tr> <th>字段</th> <th>類型</th> <th>空</th> <th>默認</th> <th>注釋</th> </tr> </thead> <tbody> {foreach $tableInfo['table_fields'] as $field} <tr> <td> {$field.name} </td> <td> {$field.type} </td> <td> {$field.null} </td> <td style="max-width: 30px;"> {$field.default} </td> <td> {$field.comment} </td> </tr> {/foreach} </tbody> </table> {/foreach} </body> </html>
以上是thinkphp5.0框架如何生成數據庫字典的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。