本文介紹了PDO 多次獲取同一查詢的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我正在使用 PDO::FETCH_ASSOC 從數據庫中獲取結果.問題是我在同一個文件中做了兩次.這是一個已知問題嗎?替代方案是什么?
I am fetching the results from a db with PDO::FETCH_ASSOC. The issue is that I am doing it twice in the same file. Is that a known issue? What would be the alternative?
這是我的代碼:
FIRST TIME
while($row = $ordersQuery->fetch(PDO::FETCH_ASSOC))
{
$totalAmount += $row['clientPrice']/100;
}
echo $totalAmount;
SECOND TIME
while($row = $ordersQuery->fetch(PDO::FETCH_ASSOC))
{
....
}
每當我刪除第一個提取時,第二個都可以正常工作.如果我有兩個,第二個不會返回任何東西.
Whenever I remove the first fetching, the second works fine. If I have both, the second does not return anything.
謝謝!
推薦答案
你不能像這樣從數據庫中多次獲取.改為這樣做:
You can't fetch from the DB multiple times like this. Do this instead:
$orders = $ordersQuery->fetchAll(PDO::FETCH_ASSOC);
...
foreach ($orders as $val) {
// stuff 1
}
...
foreach ($orders as $val) {
// stuff 2
}
這篇關于PDO 多次獲取同一查詢的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!