检查这是否是while循环中的最后一行

时间:2015-09-05 18:47:06

标签: php

我遇到这种情况:

from selenium import webdriver

fp = webdriver.FirefoxProfile()
fp.set_preference("network.cookie.cookieBehavior", 2)

browser = webdriver.Firefox(firefox_profile=fp)

是否可以检查''在不知道行数的情况下循环它是否是最后一行?

3 个答案:

答案 0 :(得分:2)

易:

$last = false;
while ($row = pg_fetch_row($result)) {

    $last = $row;

}
if ( $last !== false )
{
   // this is the last row:

}

没有其他方法可以单步执行整个结果集并保留记录。余数将是最后一个,如果没有记录则为假。 像往常一样,结果集不知道后面有多少行。当有许多记录时,...fetch_all(...)可能以内存溢出结束。

答案 1 :(得分:1)

提出类似的问题here

看起来你可以检索行数,然后只比较你的当前值(可能保留你的迭代计数并将其用作行号)。

$ number = mysql_num_rows($ sql);

编辑:在您的情况下,您似乎想要使用:pg_num_rows

答案 2 :(得分:1)

哦,只是即兴:

$curr = pg_fetch_row($result);
while ($curr) {
    $next = pg_fetch_row($result);
    if ($next) {
        echo 'no, there is more rows';
    } else {
        echo 'yes this is last row';
    }
    // use $curr anywhere _before_ this line
    $curr = $next;
}
相关问题