11

How to get Column Name With Zend DB

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
monkey_boys
  • 7,108
  • 22
  • 58
  • 82

5 Answers5

28

This is the correct answer, the older answers are wrong or outdated:

$cols = $table->info(Zend_Db_Table_Abstract::COLS); 
markus
  • 40,136
  • 23
  • 97
  • 142
corné
  • 788
  • 7
  • 14
  • 3
    rewrote 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

Bohemian
  • 412,405
  • 93
  • 575
  • 722
Paamand
  • 626
  • 1
  • 6
  • 17
0

I like this way:

$table->info('cols');
Diogo Alves
  • 378
  • 2
  • 13
0

You could use the describeTable method

Andrei Serdeliuc ॐ
  • 5,828
  • 5
  • 39
  • 66