問題描述
我正在嘗試為 PDO 語句的結(jié)果編寫一個(gè)迭代器,但我找不到任何倒帶到第一行的方法.我想避免調(diào)用 fetchAll 和存儲(chǔ)所有結(jié)果數(shù)據(jù)的開銷.
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?
推薦答案
我很確定這與數(shù)據(jù)庫有關(guān).因此,這是您應(yīng)該盡量避免的.但是,我認(rèn)為您可以通過啟用緩沖查詢來實(shí)現(xiàn)您想要的.如果這不起作用,您始終可以將結(jié)果放入帶有 fetchAll
.這兩種解決方案都會(huì)對(duì)您的應(yīng)用程序性能產(chǎn)生影響,因此如果結(jié)果集很大,請(qǐng)三思.
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.
這篇關(guān)于是否可以倒回 PDO 結(jié)果?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!