Forum Moderators: coopster

Message Too Old, No Replies

Retrieve from a query

         

gonny

3:07 pm on Dec 17, 2006 (gmt 0)

10+ Year Member



I have this query:

if (!$limit) {
$limit = 5;
}
$query = $DB->query( "SELECT n.id, n.idcategoria, n.idautore, n.titolo, n.autore, n.testo, n.data, n.totalcomm, s.id AS idcat, s.titolo AS titcat, s.icona
FROM mkp_news AS n
LEFT JOIN mkp_news_sections AS s ON(s.id = n.idcategoria)
WHERE validate = '1' ORDER BY `id` DESC LIMIT $limit");

....and this retrieve last news by id.
What I need is to retrieve not the last news but penultimate (ex. if are 30 news, I want to get the 29 -th) or the 5 -th penultimate but not the last (ex. 30)

Can help anyone?

mcibor

5:33 pm on Dec 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As I understand you, you want to show not the last, but the previous message. Am I right?

I think the best option would be to fetch the second row:

$result = mysql_query($query) or die(mysql_error());
mysql_data_seek($result, 1);
$row = mysql_fetch_assoc($result);

then if you want to reset the pointer to the beginning perform:
mysql_data_seek [php.net]($result, 0);

Regards
Michal

PS. If you want to retrieve the limit - 1 then use
mysql_data_seek($result, $limit - 2);