我希望得到一些帮助,我有一个正在尝试使用的结果集。结果包含 2 个字段和任意数量的行。
在调用 fetch_row() 时,如何判断该方法是否已到达最后一行?
我正在使用以下方法迭代行:
while(list($className, $classID) = $result->fetch_row()) {
echo "<tr><td>$className has ID $classID.</td></tr>"
// Here, if the loop is for the last time I need to allow some extra code to run
// echo "<tr><td>Last Item.</td></tr>"
}
我正在考虑在 while 循环中添加一个增量计数器,将行与 num_rows 进行比较,但我认为必须有一个光滑的计数器来告诉您是否已到达最后一行
非常感谢任何建议。 非常感谢 肖恩·麦克
最佳答案
I was thinking about having an incremental counter in the while loop which compares the row to num_rows but was thinking that there must be a slicker was of telling if you have reached the last row
不,我认为这实际上是执行此操作的唯一方法,无论您使用哪种数据库包装器(我假设它是 PDO。)
这真的很遗憾:PHP 应该内置类似的东西,至少在 foreach
构造中。但事实并非如此。
关于php - 我如何知道我是否位于数据集中的最后一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3712247/