我试图从另一个查询的结果中循环查询:
public static function getActivityFromUsers()
{
$database = DatabaseFactory::getFactory()->getConnection();
// FETCH USER_ID FOR THE SECOND QUERY
$sql = "SELECT user_id, follower_since_timestamp FROM users_followers where follower_id = :user_id";
$query = $database->prepare($sql);
$query->execute(array(':user_id' => Session::get('user_id')));
if ($query->rowCount() <= 0) {
echo 'Nothing';
return false;
}
$_MYSQL_DATA = $query->fetchAll();
/* Just for my own testing and troubleshoot */
foreach ($_MYSQL_DATA as $row) {
echo $row->user_id . " - " . date('d/m/Y H:i:s', $row->follower_since_timestamp) ."<br/>";
}
echo var_dump($_MYSQL_DATA).'<br><br>';
/* End for my own testing and troubleshoot*/
// USING THE USER_ID´s FROM THE FIRST QUERY TO FETCH DATA FOR OUTPUT
foreach ($_MYSQL_DATA as $row) {
$sql = "SELECT activity, user_id, activity_timestamp FROM user_activity WHERE user_id = :user_id";
$query = $database->prepare($sql);
$query->execute(array(':user_id' => $row->user_id));
$new_data[] = $query->fetchAll();
}
// FOREACH TO OUTPUT DATA FROM SECOND QUERY (SHOULD BE ORDERD BY activity_timestamp)
foreach($new_data as $key => $object){
echo 'User_id: ' . $object->user_id.'<br>';
echo 'What: ' . $object->activity .'<br>';
echo 'When: ' . date('d/m/Y H:i:s', $object->activity_timestamp) .'<br>';
}
echo var_dump($new_data);
}
但是这给了我错误:Notice: Trying to get property of non-object in .. User_id:
等等。
$new_data[]
的var_dump(来自第二个获取数据的查询)给出了这个:(注意底部的两个数组,为什么呢?array [2]&amp; array [3])< / p>
array(4) {
[0]=> array(7) {
[0]=> object(stdClass)#7 (3) { ["activity"]=> string(15) "Wrote" ["user_id"]=> string(1) "2" ["activity_timestamp"]=> string(10) "1433704851" }
[1]=> object(stdClass)#17 (3) { ["activity"]=> string(15) "Wrote" ["user_id"]=> string(1) "2" ["activity_timestamp"]=> string(10) "1433832032" }
[2]=> object(stdClass)#18 (3) { ["activity"]=> string(15) "Wrote" ["user_id"]=> string(1) "2" ["activity_timestamp"]=> string(10) "1433832033" }
[3]=> object(stdClass)#19 (3) { ["activity"]=> string(15) "Wrote" ["user_id"]=> string(1) "2" ["activity_timestamp"]=> string(10) "1433832035" }
[4]=> object(stdClass)#20 (3) { ["activity"]=> string(7) "Follows" ["user_id"]=> string(1) "2" ["activity_timestamp"]=> string(10) "1433832150" }
[5]=> object(stdClass)#21 (3) { ["activity"]=> string(7) "Wrote" ["user_id"]=> string(1) "2" ["activity_timestamp"]=> NULL }
[6]=> object(stdClass)#22 (3) { ["activity"]=> string(8) "Wrote " ["user_id"]=> string(1) "2" ["activity_timestamp"]=> NULL } }
[1]=> array(1) {
[0]=> object(stdClass)#16 (3) { ["activity"]=> string(19) "Finally!" ["user_id"]=> string(1) "3" ["activity_timestamp"]=> string(10) "1433794873" } }
[2]=> array(0) { }
[3]=> array(0) { } }
如何正确预测第二个查询的结果?
答案 0 :(得分:4)
fetchAll返回一个数组,而不是一个对象。
替换所有
$object->xxx
通过
$object['xxx']
答案 1 :(得分:3)
为什么不使用单个查询..
SELECT
users_followers.user_id,
users_followers.follower_since_timestamp,
a.activity,
a.user_id,
a.activity_timestamp
FROM
users_followers
LEFT JOIN
(SELECT activity, user_id, activity_timestamp FROM user_activity) a
ON a.user_id = users_followers.user_id
where
users_followers.follower_id = :user_id