php pdo:获取表的列名
如何使用PDO从表中获取所有列名?
id name age1 Alan 35
2 Alex 52
3 Amy 15
我想获得的信息是,
id name age
编辑:
这是我的尝试,
$db = $connection->get_connection();$select = $db->query('SELECT * FROM contacts');
$total_column = $select->columnCount();
var_dump($total_column);
for ($counter = 0; $counter < $total_column; $counter ++) {
$meta = $select->getColumnMeta($counter);
$column[] = $meta['name'];
}
print_r($column);
然后我得到
Array(
[0] => id
[1] => name
[2] => age
...
)
回答:
我通过以下方式解决问题(仅适用于MySQL)
$q = $dbh->prepare("DESCRIBE tablename");$q->execute();
$table_fields = $q->fetchAll(PDO::FETCH_COLUMN);
以上是 php pdo:获取表的列名 的全部内容, 来源链接: utcz.com/qa/427465.html