在MySQL中获取表列名吗?有办法获取MySQL中表的列名吗?使用php
3 回答
慕工程0101907
TA贡献1887条经验 获得超5个赞
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']
Helenr
TA贡献1780条经验 获得超4个赞
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... )
添加回答
举报
0/150
提交
取消