在PHP中使用fetchAll返回多个数组

时间:2014-03-15 01:24:06

标签: php mysql pdo

我正在尝试从数据库中检索多行。我遇到的问题是每次我使用这个函数它都会替换我已经尝试返回的其他行,它只返回最后一行。如果有人知道更好的方法或知道如何实际工作,请发表评论。

如果这在某种程度上不正确,请在此处发布新内容。感谢。

我的代码:

public function getPosts($id){
        global $pdo;

        $query = $pdo->prepare("SELECT * FROM friends WHERE f1 = ? OR f2 = ?");
        $query->bindValue(1, $id);
        $query->bindValue(2, $id);
        $query->execute();
        $res = $query->fetchAll();
            foreach($res as $user):
                if($user['f1'] == $id):
                    $query = $pdo->prepare("SELECT * FROM posts JOIN users ON posts.user_id = users.id WHERE user_id = ?");
                    $query->bindValue(1, $user['f2']);
                    $query->execute();
                else:
                    $query = $pdo->prepare("SELECT * FROM posts JOIN users ON posts.user_id = users.id WHERE user_id = ?");
                    $query->bindValue(1, $user['f1']);
                    $query->execute();
                endif;

                return $query->fetchAll(); //Trying to return arrays.
            endforeach;


}

2 个答案:

答案 0 :(得分:0)

尝试这样的事情。我不知道f1和f2是什么,所以我只是将它们添加为占位符。我在这里没有使用bindValue。使用array($f1_id, $f2_id)

调用执行更简单
public function getPosts($id){
    global $pdo;

    $f1_id = 1; //placeholder
    $f2_id = 2; //placeholder

    $posts = array();

    $stmt = $pdo->prepare("SELECT * FROM friends WHERE f1 = ? OR f2 = ?");
    $stmt->execute(array($f1_id, $f2_id));

    //Iterate over the friend records.
    //fetch() returns next record or false on failure (No more records).
    //false terminates the while loop.
    while($user = $stmt->fetch()) {
        if($user->f1 == $f1_id) :
            $stmt_post = $pdo->prepare("SELECT * FROM posts JOIN users ON posts.user_id = users.id WHERE user_id = ?");
            $stmt_post->execute(array($user['f2']));
        else:
            $stmt_post = $pdo->prepare("SELECT * FROM posts JOIN users ON posts.user_id = users.id WHERE user_id = ?");
            $stmt_post->execute(array($user->['f1']));
        endif;

        while($post = $stmt_post->fetch()) {
            $posts[] = $post; //"$posts[] = " syntax is used to append the $post to the array.
        }
    }

    return $posts;
}

答案 1 :(得分:0)

我建议发布'帖子'查询应添加到' $ res'每个用户的数组。然后返回' $ res'数组而不仅仅是特定用户的当前帖子。

未经测试的代码:

public function getPosts($id){
        global $pdo;

        $query = $pdo->prepare("SELECT * FROM friends WHERE f1 = ? OR f2 = ?");
        $query->bindValue(1, $id);
        $query->bindValue(2, $id);
        $query->execute();
        $res = $query->fetchAll();
            foreach($res as &$user): // make a reference so i can modify it
                if($user['f1'] == $id):
                    $query = $pdo->prepare("SELECT * FROM posts JOIN users ON posts.user_id = users.id WHERE user_id = ?");
                    $query->bindValue(1, $user['f2']);
                    $query->execute();
                else:
                    $query = $pdo->prepare("SELECT * FROM posts JOIN users ON posts.user_id = users.id WHERE user_id = ?");
                    $query->bindValue(1, $user['f1']);
                    $query->execute();
                endif;

                $user['posts'] = $query->fetchAll(); // save the current users posts.
            endforeach;

            return $res; // all users and their posts.

}