PDO :: FETCH_ASSOC不提取所有内容 [英] PDO::FETCH_ASSOC not fetching everything
本文介绍了PDO :: FETCH_ASSOC不提取所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$ b
函数get_following($ user_id){
global $ conn;
$ stmt = $ conn-> prepare(SELECT * FROM following following where follower_id` =:user);
$ stmt-> bindParam(':user',$ user_id,PDO :: PARAM_INT);
$ stmt-> execute();
$ following = $ stmt-> fetch(PDO :: FETCH_ASSOC);
返回$跟随;
$ / code>
| user_id | follower_id |
| 2 | 5 |
| 3 | 5 |
| 4 | 5 |
现在问题出现在我实际调用函数时,它只会从表中选择一行,我的follower_id = 5。
解决方案
$ following必须是一个行数组。你实际上只是提取第一行。使用 PDOStatement :: fetchAll()
,如下所示:
$ following = $ stmt-> fetchAll(PDO :: FETCH_ASSOC);
I have this function:
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;
}
The following
table looks like this:
|user_id|follower_id|
| 2 | 5 |
| 3 | 5 |
| 4 | 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 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不提取所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文