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

由于要写接口文档和注释,发现特别麻烦,就想写个脚手架,来自动生成

需要获取表的注释和表的字段类型,字段名,字段注释等


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:字段名,字段类型,注释等

扫描二维码关注公众号,回复: 2010335 查看本文章
$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'];
}


猜你喜欢

转载自blog.csdn.net/fish_study_csdn/article/details/75103502