Jump to content

[SOLVED] retrieve first or last record of query set...


chelnov63

Recommended Posts

hi is it possible to retrieve the last or first record of a query set, kind of hard-coding it e.g:

 


$query_rsNews = "SELECT * FROM news_tbl ORDER BY date_added DESC";
$rsNews = mysql_query($query_limit_rsNews, $myConnection) or die(mysql_error());
$row_rsNews = mysql_fetch_assoc($rsNews);

echo "first is: ".$row_rsNews[0]['id']; //trying to display id of first record here .. obviously this is wrong syntax
echo "last row is: ".  ???????????  //trying to display id of last record here .. obviously this is wrong syntax

 

thanks in advance...

while ($row = mysql_fetch_assoc($rsNews)) {

      $data[] = $row;

}

echo "First is:" . $row_rsNews[0]['id'];

echo "Last is:" . $row_rsNews[count($row_rsNews)-1]['id'];

 

There are neater ways to do it, but I'm just demonstrating. xD

thanks for that but i seem to be getting errors with the following code:

 

$query_rsNews = "SELECT * FROM news_tbl ORDER BY date_added DESC";
$query_limit_rsNews = sprintf("%s LIMIT %d, %d", $query_rsNews, $startRow_rsNews, $maxRows_rsNews);
$rsNews = mysql_query($query_limit_rsNews, $myConnection) or die(mysql_error());

while ($row = mysql_fetch_assoc($rsNews)) {
       $data[] = $row;
}

echo "First is:" . $row_rsNews[0]['id'];
echo "Last is:" . $row_rsNews[count($row_rsNews)-1]['id'];

 

the errors are

 

Notice: Undefined variable: row_rsNews in C:\htdocs\\index_flash.php on line 48
First is:
Notice: Undefined variable: row_rsNews in C:\htdocs\\index_flash.php on line 49

Notice: Undefined variable: row_rsNews in C:\htdocs\\index_flash.php on line 49
Last is: 

 

Later on in the code I need to generate a table from this recordset ..however before generating the table I need to know the last and first values of the recordset...as I am making html links which use the id values of these records..and these links are created before the table is generated..

 

thanks for your help

<?php

$query_rsNews = "SELECT * FROM news_tbl ORDER BY date_added DESC";
if ($rsNews = mysql_query($query_limit_rsNews, $myConnection)) {
  $rows = mysql_num_rows($rsnews);
  if ($rows) {
    $row_rsNews = mysql_fetch_assoc($rsNews);
    echo "First is:" . $row_rsNews['id'];
    mysql_data_seek($rsNews, $rows);
    $row_rsNews = mysql_fetch_assoc($rsNews);
    echo "Last is:" . $row_rsNews['id'];
  }
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.