Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Column names of a table Using PDO in PHP

Tags:

php

mysql

pdo

Can anybody help me regarding this... My code is below :

$STH = $DBH->query('SELECT id,zone,latlng FROM `rl_zones`');
$STH->setFetchMode(PDO::FETCH_ASSOC);

while ($row = $STH->fetch()) {
    // here i need column names i.e id,zone,latlng.
    // i want to display those column names....
}

Thanks

like image 360
Hari Krishna Avatar asked Nov 27 '25 05:11

Hari Krishna


1 Answers

IN pdo use PDO::FETCH_COLUMN

$STH = $DBH->prepare("DESCRIBE rl_zones");
$STH->execute();
$table_fields = $STH->fetchAll(PDO::FETCH_COLUMN);
print_r($table_fields);

http://php.net/manual/en/pdostatement.fetchcolumn.php

like image 62
Saty Avatar answered Nov 28 '25 21:11

Saty



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!