douguazhi5966 2016-12-18 10:33
浏览 42

从zf2中有一行的变量访问数据

I am using object of type Zend\Db\ResultSet\ResultSet which I passed from the controller to a view. Code is given below:

view/districts/index.php:

foreach ($pk_province as $pname) 
{   
    $p_id = $this->escapeHtml($pname->id);
    $p_name = $this->escapeHtml($pname->p_name);     
} 

$pk_province has single record. So I want to access data from it without loop. I tried $p_name = $pk_province->p_name but it gives me undefined variable $d_name.

Any suggestion please.

var_dump($pk_province); shows the following:

object(Zend\Db\ResultSet\ResultSet)#344 (8) {
    ["allowedReturnTypes":protected]=> array(2) { 
        [0]=> string(11) "arrayobject" 
        [1]=> string(5) "array" 
    } 
    ["arrayObjectPrototype":protected]=> object(Admin\Model\Provinces)#312 (3) { 
        ["id"]=> NULL 
        ["p_name"]=> NULL 
        ["inputFilter":protected]=> NULL 
    } 
    ["returnType":protected]=> string(11) "arrayobject"  
    ["buffer":protected]=> NULL 
    ["count":protected]=> int(1) 
    ["dataSource":protected]=> object(Zend\Db\Adapter\Driver\Pdo\Result)#322 (9) {
        ["statementMode":protected]=> string(7) "forward" 
        ["fetchMode":protected]=> int(2) 
        ["resource":protected]=> object(PDOStatement)#316 (1) {
            ["queryString"]=> string(58) "SELECT `provinces`.* FROM `provinces` WHERE `id` = :where1" 
        } 
        ["options":protected]=> NULL 
        ["currentComplete":protected]=> bool(false) 
        ["currentData":protected]=> NULL 
        ["position":protected]=> int(-1) 
        ["generatedValue":protected]=> string(1) "0"  
        ["rowCount":protected]=> int(1) 
    } 
    ["fieldCount":protected]=> int(2) 
    ["position":protected]=> int(0) 
}
  • 写回答

1条回答 默认 最新

  • douxiu6835 2016-12-19 10:43
    关注

    You can convert the ResultSet to an array using the ResultSet::toArray method and get the first row from your array:

    $array = $resultSet->toArray();
    $province = $array[0];
    

    Or you can use the ResultSet::current method to collect the current row:

    $province = $resultSet->current();
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度