您的位置:首页 > 编程语言 > PHP开发

php获取数据库表的相关信息,

2017-07-14 11:51 441 查看
由于要写接口文档和注释,发现特别麻烦,就想写个脚手架,来自动生成需要获取表的注释和表的字段类型,字段名,字段注释等
1:获取表注释
show 
table
 
status 
like
 
表名
读取表名和注释
$sql   = sprintf("show table status like '%s'", $table);$stmt  = $dbh->prepare($sql);$stmt->execute();$results = $stmt->fetchAll(PDO::FETCH_ASSOC);if(!empty($results)) {if($results[0]['Name'] == $table){$sComment = $results[0]['Comment'];}}
2:字段名,字段类型,注释等
$sql   = sprintf('SHOW FULL COLUMNS FROM %s', $table);$stmt  = $dbh->prepare($sql);$stmt->execute();$results = $stmt->fetchAll(PDO::FETCH_ASSOC);$fields  = array();foreach ($results as $row) {if (in_array($row['Field'], $filter_columns)) {continue;}$fields[$row['Field']]['fieldName'] = $row['Field'];$length = strpos($row['Type'],'(');if($length === false) {$length = strlen($row['Type']);}$val_type = substr($row['Type'], 0, $length);$fields[$row['Field']]['Type'] = field_format($val_type);$fields[$row['Field']]['Comment'] = $row['Comment'];}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  php 数据库