問題描述
我正在嘗試為 PDO 語句的結果編寫一個迭代器,但我找不到任何倒帶到第一行的方法.我想避免調用 fetchAll 和存儲所有結果數據的開銷.
I'm trying to write an iterator for results from a PDO statement but I can't find any way of rewinding to the first row. I would like to avoid the overhead of calling fetchAll and storing all the result data.
// first loop works fine
foreach($statement as $result) {
// do something with result
}
// but subsequent loops don't
foreach($statement as $result) {
// never called
}
有什么方法可以重置語句或查找第一行嗎?
Is there some way of reseting the statement or seeking the first row?
推薦答案
我很確定這與數據庫有關.因此,這是您應該盡量避免的.但是,我認為您可以通過啟用緩沖查詢來實現您想要的.如果這不起作用,您始終可以將結果放入帶有 fetchAll
.這兩種解決方案都會對您的應用程序性能產生影響,因此如果結果集很大,請三思.
I'm pretty sure this is database dependent. Because of that, it is something you should try to avoid. However, I think you can achieve what you want by enabling buffered queries. If that doesn't work, you can always pull the result into an array with fetchAll
. Both solutions have implications for your applications performance, so think twice about it, if the resultsets are large.
這篇關于是否可以倒回 PDO 結果?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!