-
饮歌长啸
以下SQL语句几乎等效:SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'tbl_name'
[AND table_schema = 'db_name']
[AND column_name LIKE 'wild']SHOW COLUMNS
FROM tbl_name[FROM db_name][LIKE 'wild']参考资料:信息模式列
-
慕森王
我创建了一个PDO函数,它返回一个简单数组中的所有列名。public function getColumnNames($table){
$sql = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = :table";
try {
$core = Core::getInstance();
$stmt = $core->dbh->prepare($sql);
$stmt->bindValue(':table', $table, PDO::PARAM_STR);
$stmt->execute();
$output = array();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$output[] = $row['COLUMN_NAME'];
}
return $output;
}
catch(PDOException $pe) {
trigger_error('Could not connect to MySQL database. ' . $pe->getMessage() , E_USER_ERROR);
}}输出将是一个数组:Array ([0] => id[1] => name[2] => email[3] => shoe_size[4] => likes... )对不起,我喜欢我的功能;)我还没有包括核心类,但是你可以使用你自己的类。D.S.
-
一只甜甜圈
你可以用描述:DESCRIBE my_table;或者在较新的版本中,您可以使用信息图式:SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'my_database' AND TABLE_NAME = 'my_table';或者你可以用显示列:SHOW COLUMNS FROM my_table;