开发者

MySQL consequent numbers

I use this function in my while loop to track the numbers of results.

$lvarcontributor = "SELECT * FROM  dD_Contributors WHERE idAlbum = '".$idAlbum."' ORDER BY idContributor ASC";
$lvarresult=开发者_JS百科mysql_query($lvarcontributor);
$var = 0;
while ($lvarrow = mysql_fetch_array($lvarresult))
    {
    $var++;
//STUFF HERE...
}

How do I know the last result? I need to know when $var++ is arrived at the last result. such as

If($var == "LAST RESULT") {
//do this
}

THANKS!


Use mysql_num_rows function - http://php.net/manual/en/function.mysql-num-rows.php


This function may be helpfull:

mysql_num_rows

if (($var + 1) == musql_num_rows($result))
{
   //this is the last row
}


if( $var == mysql_num_rows($lvarresult) - 1 ) {
    // this is the last row, do your thing
}


Maybe you want to check the mysql_num_rows function. If you want to access only the last result, you should review your SQL query to fetch only this one (try messing with ORDER BY and LIMIT).

If you want to fetch every rows, but have a special treatment for the last result, then you can dut as follow:

$nextRow = mysql_fetch_array($lvarresult);
while ($nextRow)
{
   $currentRow = $nextRow;
   if($nextRow = mysql_fetch_array($lvarresult)) {
        //Not the last row, data in $currentRow
   }
   else {
        //Last row, data in $currentRow
   }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜