本文介绍了PDO::FETCH_ASSOC 未获取所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个功能:

function get_following($user_id) {
 global $conn;
 $stmt = $conn->prepare("SELECT * FROM following WHERE `follower_id`=:user");
 $stmt->bindParam(':user', $user_id, PDO::PARAM_INT);
 $stmt->execute();
 $following =$stmt->fetch(PDO::FETCH_ASSOC);
 return $following;
}

以下表格如下所示:

|user_id|follower_id|
|   2   |     5     |
|   3   |     5     |
|   4   |     5     |

现在的问题是,当我实际调用该函数时,它只从表中选择一行,其中我的 follower_id = 5.

Now the problem is when I actually call the function it only selects one of the rows from the table, where my follower_id = 5.

推荐答案

$following 必须是一个行数组.您实际上只是在获取第一行.使用 PDOStatement::fetchAll() 获取它,如下所示:

$following will have to be an array of rows. You are actually only fetching the first row. Fetch it using PDOStatement::fetchAll(), like this:

$following = $stmt->fetchAll(PDO::FETCH_ASSOC);

这篇关于PDO::FETCH_ASSOC 未获取所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 01:51