How to get Column Name With Zend DB
Asked
Active
Viewed 1.7k times
11
-
2Perhaps you should describe a little bit what you're trying to do... Apikot's answer is a starting point though. – Stefan Gehrig Apr 02 '09 at 08:31
5 Answers
28
This is the correct answer, the older answers are wrong or outdated:
$cols = $table->info(Zend_Db_Table_Abstract::COLS);
-
3rewrote your intro-text since SO doesn't know 'bumping' and questions/answers are not considered old. in fact SO is a wiki and updating it is one of the best things you can do! +1 – markus Sep 21 '09 at 09:39
8
$metadata = $db->describeTable($tableName);
$columnNames = array_keys($metadata);
http://framework.zend.com/manual/en/zend.db.html#zend.db.adapter.list-describe

vartec
- 131,205
- 36
- 218
- 244
-
That's not really 100% correct as $metadata is an associative array ('column_name' => array()) of associative arrays (one for each column of the table). – Stefan Gehrig Apr 02 '09 at 08:34
-
add schame's name to $db->describeTable($tableName, $schema); , i just had an error using oracle which takes old column names if i had changed them and had to specify the schema for it to return right column names – Mouna Cheikhna Mar 05 '12 at 13:34
2
Previous answer applies only to version < 2.
For current version of ZF (2.2) use:
$table = new Zend\Db\TableGateway\TableGateway('table', $Dbadapter, new Zend\Db\TableGateway\Feature\MetadataFeature());
$columns = $table->getColumns();
http://framework.zend.com/manual/2.2/en/modules/zend.db.table-gateway.html#tablegateway-features http://framework.zend.com/manual/2.2/en/modules/zend.db.metadata.html