我在php中有一个代码:
$result = mysql_query($cxn,$sql_query)
$dataset= mysqli_fetch_fields($result);
for ($i = 0; $i < 13; $i++) {
$dataset[$i]->name
}
我想要zend中的上述代码。
这里mysql_fetch_fields($result)返回给定结果集$result中的字段信息。如何在zend框架中做到这一点?我在谷歌上搜索过,我发现我们可以从特定的表中检索关于列的信息,但从结果集中如何在zend框架中检索??
这在Zend Framework中目前是不可能的。查看结果集元数据的请求解决方案。您可以尝试使用实验性PDOStatement::getColumnMeta
UPDATE-注释中的代码示例
sample table structure
table1: id (int), field1 char(3)
table2: id (int), field2 char(3)
<?php
require_once('Zend/Loader/Autoloader.php');
$autoloader = Zend_Loader_Autoloader::getInstance();
// create MySQL database adapter
$db = new Zend_Db_Adapter_Pdo_Mysql(array(
'host' => '127.0.0.1',
'username' => 'test',
'password' => 'test',
'dbname' => 'test'
));
// create temporary table
$result = $db->getConnection()->exec('
CREATE TEMPORARY TABLE myTable
SELECT
t1.id,
t1.field1,
t2.field2
FROM table1 t1
INNER JOIN table2 t2
ON t1.id = t2.id
');
// describe
$info = $db->describeTable('myTable');
var_dump($info);
// drop table
$result = $db->getConnection()->exec('DROP TEMPORARY TABLE myTable');
有关运行"其他"数据库语句的更多信息
我发现使用Zend Framework获取关于表的所有元数据非常简单。
- 首先为表创建DbTable模型:定义表类
- 实例化对象:
$table = new Application_Model_DbTable_Table();
- 对该对象调用->info():$meta=$table->info(
以下是使用此方法的转储摘录:检索表元数据
array(10) {
["schema"] => NULL
["name"] => string(5) "track"
["cols"] => array(6) {
[0] => string(7) "trackid"
[1] => string(9) "weekendid"
[2] => string(7) "shiftid"
[3] => string(13) "bidlocationid"
[4] => string(3) "qty"
[5] => string(4) "lead"
}
["primary"] => array(1) {
[1] => string(7) "trackid"
}
["metadata"] => array(6) {
["trackid"] => array(14) {
["SCHEMA_NAME"] => NULL
["TABLE_NAME"] => string(5) "track"
["COLUMN_NAME"] => string(7) "trackid"
["COLUMN_POSITION"] => int(1)
["DATA_TYPE"] => string(8) "smallint"
["DEFAULT"] => NULL
["NULLABLE"] => bool(false)
["LENGTH"] => NULL
["SCALE"] => NULL
["PRECISION"] => NULL
["UNSIGNED"] => NULL
["PRIMARY"] => bool(true)
["PRIMARY_POSITION"] => int(1)
["IDENTITY"] => bool(true)
}
... cont ...
}
["rowClass"] => string(27) "Application_Model_Row_Track"
["rowsetClass"] => string(20) "Zend_Db_Table_Rowset"
["referenceMap"] => array(3) {
["Weekend"] => array(3) {
["columns"] => string(9) "weekendid"
["refTableClass"] => string(33) "Application_Model_DbTable_Weekend"
["refColumns"] => string(9) "weekendid"
}
["Shift"] => array(3) {
["columns"] => string(7) "shiftid"
["refTableClass"] => string(31) "Application_Model_DbTable_Shift"
["refColumns"] => string(7) "shiftid"
}
["BidLocation"] => array(3) {
["columns"] => string(13) "bidlocationid"
["refTableClass"] => string(37) "Application_Model_DbTable_BidLocation"
["refColumns"] => string(13) "bidlocationid"
}
}
["dependentTables"] => array(1) {
[0] => string(32) "Application_Model_DbTable_Member"
}
["sequence"] => bool(true)
}