php mysql查询表的各字段名以及备注

业务场景:可以通过程序实现动态的数据字典的功能。

原理就是页面所用的表,查询出表各字段的名字以及备注

SELECT COLUMN_NAME, DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT, COLUMN_COMMENT from information_schema.columns where table_schema = 'db' and table_name='table';

 方法二

/**
 * 取得数据表的字段信息
 * @access public
 * @return array
 */
public function getFields($tableName) {
    $result = $this->query('SHOW FULL COLUMNS FROM ' . $tableName);
    $columns = array();
    foreach ($result as $val) {
        $columns[$val['field']] = array(
            'name' => $val['field'],
            'type' => $val['type'],
            'notnull' => (bool)($val['null'] === ''), // not null is empty, null is yes
            'default' => $val['default'],
            'comment' => $val['comment'],
            'primary' => (strtolower($val['key']) == 'pri'),
            'autoinc' => (strtolower($val['extra']) == 'auto_increment'),
        );
    }
    return $columns;
}

猜你喜欢

转载自hudeyong926.iteye.com/blog/2380734