chelnov63 Posted August 6, 2008 Share Posted August 6, 2008 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... Link to comment https://forums.phpfreaks.com/topic/118472-solved-retrieve-first-or-last-record-of-query-set/ Share on other sites More sharing options...
DarkWater Posted August 6, 2008 Share Posted August 6, 2008 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 Link to comment https://forums.phpfreaks.com/topic/118472-solved-retrieve-first-or-last-record-of-query-set/#findComment-609877 Share on other sites More sharing options...
chelnov63 Posted August 6, 2008 Author Share Posted August 6, 2008 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 Link to comment https://forums.phpfreaks.com/topic/118472-solved-retrieve-first-or-last-record-of-query-set/#findComment-609885 Share on other sites More sharing options...
Jabop Posted August 6, 2008 Share Posted August 6, 2008 You could do two separate selects if you think the queries aren't too large to separate them without a much of a purpose... First: ORDER BY ID DESC LIMIT 1 Last: ORDER BY ID ASC LIMIT 1 Link to comment https://forums.phpfreaks.com/topic/118472-solved-retrieve-first-or-last-record-of-query-set/#findComment-609911 Share on other sites More sharing options...
trq Posted August 6, 2008 Share Posted August 6, 2008 <?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']; } } ?> Link to comment https://forums.phpfreaks.com/topic/118472-solved-retrieve-first-or-last-record-of-query-set/#findComment-609923 Share on other sites More sharing options...
DarkWater Posted August 6, 2008 Share Posted August 6, 2008 Ah, there we go, mysql_data_seek. Good call (I forgot about using that). Link to comment https://forums.phpfreaks.com/topic/118472-solved-retrieve-first-or-last-record-of-query-set/#findComment-610047 Share on other sites More sharing options...
chelnov63 Posted August 10, 2008 Author Share Posted August 10, 2008 sorry abou the late reply... thanks for your help guys..ur life savers..really appreciate it Link to comment https://forums.phpfreaks.com/topic/118472-solved-retrieve-first-or-last-record-of-query-set/#findComment-612859 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.