19

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.

Saint Robson
  • 5,475
  • 18
  • 71
  • 118
  • 1
    Please, don't use `mysql_*` functions for new code. They are no longer maintained and the community has begun the [deprecation process](http://goo.gl/KJveJ). See the [**red box**](http://php.net/manual/en/function.mysql-pconnect.php)? Instead you should learn about [prepared statements](http://goo.gl/vn8zQ) and use either [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli). If you can't decide, [this article](http://goo.gl/3gqF9) will help to choose. If you care to learn, [here is a good PDO tutorial](http://goo.gl/vFWnC). – Mihai Iorga Aug 25 '12 at 17:06
  • It's the `/* Do Something Here*/` that matters here. Are you mutating the data or are you printing data. There may be a better way to achieve what you want without performing iterated conditions. – mickmackusa Aug 22 '21 at 05:48

6 Answers6

50

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

$numResults = mysqli_num_rows($result);
$counter = 0
while ($row = mysqli_fetch_array($result)) {
    if (++$counter == $numResults) {
        // last row
    } else {
        // not last row
    }
}
Alberto Perez
  • 1,019
  • 15
  • 17
newfurniturey
  • 37,556
  • 9
  • 94
  • 102
11
$result = mysql_query("SELECT *SOMETHING* ");

$i = 1;
$allRows = mysql_num_rows($result);
while($row = mysql_fetch_array($result)){

    if ($allRows == $i) {
        /* Do Something Here*/
    } else {
        /* Do Another Thing Here*/}
    }
    $i++;
}

but please take in consideration PDO

$db = new PDO('mysql:host=localhost;dbname=testdb', 'username', 'password');
$stmt = $db->query("SELECT * FROM table");
$allRows = $stmt->rowCount();
$i = 1;
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {

    if ($allRows == $i) {
        /* Do Something Here*/
    } else {
        /* Do Another Thing Here*/}
    }
    $i++;
}
Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107
3
$allRows = $stmt->rowCount();

Didn't work for me, had to use:

$numResults = $result->num_rows;
eljoe
  • 168
  • 2
  • 7
2

Try this:

$result = mysql_query("SELECT colum_name, COUNT(*) AS `count` FROM table");

$i = 0;

while($row = mysql_fetch_assoc($result))
{
    $i++;

    if($i == $row['count'])
    {
        echo 'last row';
    }
    else
    {
        echo 'not last row';
    }
}
snuffn
  • 2,102
  • 13
  • 15
0

Try having a counter inside the while loop and then checking it against mysql_num_rows()

DMIL
  • 693
  • 3
  • 7
  • 18
0
$result = //array from the result of sql query.
$key = 1000; 
$row_count = count($result); 
if($key)
{
    if($key == $row_count-1)  //array start from 0, so we need to subtract.
    {
       echo "Last row";             
    }
    else
    {
       echo "Still rows are there";             
    }
}
Pavan BS
  • 11
  • 4