本文介紹了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 未獲取所有內容的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!