为什么要赢得我的foreach工作?

时间:2015-06-06 03:27:53

标签: php pdo foreach

$subSpecialties = $this->_db->get('users_subspecialties',array('user_id', '=', $this->_data->id));

print_r($subSpecialties);

foreach ($subSpecialties as $subSpecialty) {
    echo "THIS CODE IS GENERATED FROM INSIDE FOREACH";
}

第一行从我的users_subspecialties

获取记录

然后我使用print_r检查找到的记录。

输出如下所示:

DB Object ( [_pdo:DB:private] => PDO Object ( ) [_query:DB:private] 
=> PDOStatement Object ( [queryString] => SELECT * FROM 
users_subspecialties WHERE user_id = ? ) [_error:DB:private] => 
[_results:DB:private] => Array ( [0] => stdClass Object ( [user_id] 
=> 1 [subspecialty_id] => 2 ) [1] => stdClass Object ( [user_id] => 1 
[subspecialty_id] => 3 ) ) [_count:DB:private] => 2 )

最后一行是我for-each;它应该运行,因为您可以通过print_r输出找到2条记录,但它赢得了echo内部代码。

有人可以告诉我看起来是什么问题吗?提前谢谢。

2 个答案:

答案 0 :(得分:1)

如果这个回答是对象,你可以这样:

foreach ($subSpecialties as $subSpecialty) {
   echo $subSpecialty->user_id;
   echo '<br/>';
   echo $subSpecialty->subspecialty_id;
}

如果此响应是数组,那么

foreach ($subSpecialties as $subSpecialty) {
   echo $subSpecialty[user_id];
   echo '<br/>';
   echo $subSpecialty[subspecialty_id];
}

答案 1 :(得分:1)

$subSpecialties = $this->_db->get('users_subspecialties',array('user_id', '=', $this->_data->id));

print_r($subSpecialties);

foreach ($subSpecialties->results() as $subSpecialty) {
    echo "THIS CODE IS GENERATED FROM INSIDE FOREACH";
}