错误显示试图获取非对象的属性

时间:2014-03-07 06:02:53

标签: yii

$sensor = Yii::app()->zdb->createCommand("select * from tbl_sensor where stype=2 and nid=$nid order by timestamp desc limit 10")->queryAll(); 

if($sensor)

 { return $sensor->sdata/10; } 

else 

{ return null; } 

2 个答案:

答案 0 :(得分:0)

QueryAll是返回数组。像这样使用

foreach($sensor as $key => $row) { 
     $yourvar = $row['sdata'];
 }

答案 1 :(得分:0)

QueryAll将为您提供数组数组..

获取所有列:

$sensor = Yii::app()->zdb->createCommand("select * from tbl_sensor where stype=2 and nid=$nid  order by timestamp desc limit 10")->queryAll(); 
if($sensor) { 
    foreach($sensor as $data_row) {
        foreach($data_row as $data_key => $data_value) { 
             echo "$data_key: $data_value\n";
        }
    }
} 
else { 
    return null; 
}

获取sdata:

$sdata_values = Yii::app()->zdb->createCommand("select sdata from tbl_sensor where stype=2 and nid=$nid  order by timestamp desc limit 10")->queryColumn(); 
if($sdata_values) { 
    foreach($sdata_values as $sdata_value) {
        echo $sdata_value / 10;
    }
} 
else { 
    return null; 
}