How to Get Field Name With Query in Zend Framework Test "Select * From Test1,Test2" how to get all field name in this query Zend Frame work cam do it?
+2
A:
Untested, but I believe the query is returned as an associative array (where the column name is the key), and so you can loop through the first record and pick up the column names, e.g.
$sql = 'Select * From Test1,Test2';
$result = $db->fetchAll($sql, 2);
foreach ($result[0] as $key => $value) {
echo $key;
...
}
Alistair Knock
2009-04-03 09:03:34
but not smart $result[0] will have lest 1 data in rowif don't have ,it wiill error :<
monkey_boys
2009-04-17 04:45:20
i will insert and delete for this ?
monkey_boys
2009-04-17 04:48:03
It'd be expensive and inefficient to create a dummy row for this purpose (3 queries), so you probably want to test the number of rows returned, and if it's zero, use one of the meta techniques Till describes. Or better still, don't use 'select *' as it's not best practice; find out only the field names in advance (e.g. using Till's technique) and then use that to build your data queries.
Alistair Knock
2009-04-17 08:09:10
But Zend dont't have fetch_field function same codeigniter better :<
monkey_boys
2009-04-17 09:55:56
+1
A:
You could also issue $db->describeTable('Test1')
, etc. before or after the query which would provide you with all the meta information you need. This query is pretty expensive though, so make sure to cache the response.
Also, if you're using a model which extends Zend_Db_Table_Abstract
, then you should have all this information already. In this case, all you need to do is access the protected property $_metadata
.
HTH
Till
2009-04-11 13:19:51