Php 无法从zf3中的ResultSet对象数组中获取数据
我正在使用zend framework3和数据库的Mysql驱动程序。 我正在使用以下代码获取数据:Php 无法从zf3中的ResultSet对象数组中获取数据,php,zend-framework3,Php,Zend Framework3,我正在使用zend framework3和数据库的Mysql驱动程序。 我正在使用以下代码获取数据: $con = $this->adapter; $select = $this->sql->select(); $select->from('nav_menu'); $selectString = $this->sql->getSqlStringForSqlObject($select);
$con = $this->adapter;
$select = $this->sql->select();
$select->from('nav_menu');
$selectString = $this->sql->getSqlStringForSqlObject($select);
$results = $con->query($selectString, $con::QUERY_MODE_EXECUTE);
$resultSet = new ResultSet();
$resultSet->initialize($results);
当我var_转储此数据时,我得到的结果如下:
Zend\Db\ResultSet\ResultSet Object
(
[allowedReturnTypes:protected] => Array
(
[0] => arrayobject
[1] => array
)
[arrayObjectPrototype:protected] => ArrayObject Object
(
[storage:ArrayObject:private] => Array
(
)
)
[returnType:protected] => arrayobject
[buffer:protected] =>
[count:protected] =>
[dataSource:protected] => Zend\Db\ResultSet\ResultSet Object
(
[allowedReturnTypes:protected] => Array
(
[0] => arrayobject
[1] => array
)
[arrayObjectPrototype:protected] => ArrayObject Object
(
[storage:ArrayObject:private] => Array
(
)
)
[returnType:protected] => arrayobject
[buffer:protected] => -1
[count:protected] =>
[dataSource:protected] => Zend\Db\Adapter\Driver\Mysqli\Result Object
(
[resource:protected] => mysqli_result Object
(
[current_field] => 0
[field_count] => 8
[lengths] =>
[num_rows] => 15
[type] => 0
)
[isBuffered:protected] => 1
[position:protected] => 0
[numberOfRows:protected] => -1
[currentComplete:protected] =>
[nextComplete:protected] =>
[currentData:protected] =>
[statementBindValues:protected] => Array
(
[keys] =>
[values] => Array
(
)
)
[generatedValue:protected] => 0
)
[fieldCount:protected] => 8
[position:protected] => 0
)
[fieldCount:protected] =>
[position:protected] => 0
)
foreach ($resultSet as $key => $value) {
$array[$i]['id'] = $value->id;
$array[$i]['name'] = $value->name;
$array[$i]['label'] = $value->label;
$array[$i]['route'] = $value->route;
$array[$i]['parent_id'] = $value->parent_id;
$i++;
}
当我重复此操作以获得如下值时,我得到一个错误:
Zend\Db\ResultSet\ResultSet Object
(
[allowedReturnTypes:protected] => Array
(
[0] => arrayobject
[1] => array
)
[arrayObjectPrototype:protected] => ArrayObject Object
(
[storage:ArrayObject:private] => Array
(
)
)
[returnType:protected] => arrayobject
[buffer:protected] =>
[count:protected] =>
[dataSource:protected] => Zend\Db\ResultSet\ResultSet Object
(
[allowedReturnTypes:protected] => Array
(
[0] => arrayobject
[1] => array
)
[arrayObjectPrototype:protected] => ArrayObject Object
(
[storage:ArrayObject:private] => Array
(
)
)
[returnType:protected] => arrayobject
[buffer:protected] => -1
[count:protected] =>
[dataSource:protected] => Zend\Db\Adapter\Driver\Mysqli\Result Object
(
[resource:protected] => mysqli_result Object
(
[current_field] => 0
[field_count] => 8
[lengths] =>
[num_rows] => 15
[type] => 0
)
[isBuffered:protected] => 1
[position:protected] => 0
[numberOfRows:protected] => -1
[currentComplete:protected] =>
[nextComplete:protected] =>
[currentData:protected] =>
[statementBindValues:protected] => Array
(
[keys] =>
[values] => Array
(
)
)
[generatedValue:protected] => 0
)
[fieldCount:protected] => 8
[position:protected] => 0
)
[fieldCount:protected] =>
[position:protected] => 0
)
foreach ($resultSet as $key => $value) {
$array[$i]['id'] = $value->id;
$array[$i]['name'] = $value->name;
$array[$i]['label'] = $value->label;
$array[$i]['route'] = $value->route;
$array[$i]['parent_id'] = $value->parent_id;
$i++;
}
我不知道我错在哪里。当有num_行时,行数为15。这在ZF2中运行良好。
感谢任何人的帮助
我尝试了很多,发现如果替换代码
$selectString = $this->sql->getSqlStringForSqlObject($select);
$results = $con->query($selectString, $con::QUERY_MODE_EXECUTE);
和
$statement = $this->sql->prepareStatementForSqlObject($select);
$results = $statement->execute();
然后我得到了结果。但是我的问题是为什么我没有通过测试
$selectString = $this->sql->getSqlStringForSqlObject($select);
$results = $con->query($selectString, $con::QUERY_MODE_EXECUTE);
getSqlStringForSqlObject
自2.4版以来已被弃用。但是我不知道这是否是它不再工作的原因
根据研究,有两种方法可以实现这一点。使用与您使用的完全相同的prepare语句:
$statement = $sql->prepareStatementForSqlObject($select);
$results = $statement->execute();
执行查询时,您可能正在查找:
$selectString = $sql->buildSqlString($select);
$results = $adapter->query($selectString, $adapter::QUERY_MODE_EXECUTE);
你知道怎么调用程序吗。我正在使用这个代码$results=$con->query('CALL
fetching_menu
(?)',$spParams);$resultSet=新结果集;$resultSet->initialize($results);其中$spParams=array('backend');我出错了,我解决了这个问题。我使用$spParams=['backend'];$statement=$con->createStatement('CALLfetching_menu
(?)',$spParams);$result=$statement->execute();$resultSet=新结果集;$resultSet->initialize($result);