PHP检查MySQL最后一行

问题描述:

我有一个关于PHP的简单问题,以检查这是否是MySQL的最后一行.例如,我有以下代码:

I have a simple question regarding PHP to check if this is the last row of MySQL or not. For example I have this code :

$result = mysql_query("SELECT *SOMETHING* ");

while($row = mysql_fetch_array($result))
{
if (*this is the last row*)
{/* Do Something Here*/}
else
{/* Do Another Thing Here*/}
}

我很难检查该行是否为最后一行.知道怎么做吗?谢谢.

I have difficulties to check if the row is the last one or not. Any idea how to do it? Thanks.

您可以在while循环之前使用 mysql_num_rows() ,然后根据您的条件使用该值:

You can use mysql_num_rows() prior to your while loop, and then use that value for your condition:

$numResults = mysql_num_rows($result);
$counter = 0
while ($row = mysql_fetch_array($result)) {
    if (++$counter == $numResults) {
        // last row
    } else {
        // not last row
    }
}