3

Im trying to loop through a query from the result of another query:

    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);
    }

But this gives me the error: Notice: Trying to get property of non-object in .. User_id: etc..

The var_dump of $new_data[] (from the second query that fetched the data) gives me this: (notice the two arrays in the bottom, why is that? array[2] & array[3])

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) { } }

How do I properly foreach the result from the second query?

2 Answers 2

4

fetchAll returns an Array, not an object.

Replace all your

$object->xxx

by

$object['xxx']
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for you time and effort!
3

Why not just use a single query..

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.