By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,744 Members | 1,716 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,744 IT Pros & Developers. It's quick & easy.

Next and Previous record

P: n/a
I am moving throught a set of records, my next works great but previous move
to the first record. Also what is the best way to check and see if it is the
first or last record in the set.

if($move == "Next")
{
$next_rec = "select * from people where contactid > $rec_id limit 1";
$results = mysql_query($next_rec);
$row = mysql_fetch_row($results);
include("http://localhost/keith-homes/hyperlink.php?val=$row[0]");
exit;
}
elseif($move == "Previous")
{
$next_rec = "select * from people where contactid < $rec_id limit 1";
$results = mysql_query($next_rec);
$row = mysql_fetch_row($results);
include("http://localhost/keith-homes/hyperlink.php?val=$row[0]");
exit;
}


Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
I understand why it goes to the first record and would like a better way to
navigate backwards through the set. It starts it query at the first record
and the first is less than the current ID so it stops there. Correct.
"Rick" <vf*****@cox.net> wrote in message
news:xmerb.7185$Zb7.6789@fed1read01...
I am moving throught a set of records, my next works great but previous move to the first record. Also what is the best way to check and see if it is the first or last record in the set.

if($move == "Next")
{
$next_rec = "select * from people where contactid > $rec_id limit 1";
$results = mysql_query($next_rec);
$row = mysql_fetch_row($results);
include("http://localhost/keith-homes/hyperlink.php?val=$row[0]");
exit;
}
elseif($move == "Previous")
{
$next_rec = "select * from people where contactid < $rec_id limit 1";
$results = mysql_query($next_rec);
$row = mysql_fetch_row($results);
include("http://localhost/keith-homes/hyperlink.php?val=$row[0]");
exit;
}


Jul 17 '05 #2

P: n/a
Mr. Clean, speaking through Rick, wrote:

$next_rec = "select * from people where contactid > $rec_id limit 1";
try:

SELECT * FROM people
WHERE contactid > $rec_id
ORDER BY contactid
LIMIT 1
$next_rec = "select * from people where contactid < $rec_id limit 1";


SELECT * FROM people
WHERE contactid < $rec_id
ORDER BY contactid DESC
LIMIT 1

/joe
--
In lbbs, El Scorcho's plain radio from Robert Jameson is blue?? Krispy
Kreme is retarded.
Jul 17 '05 #3

P: n/a
On Sat, 08 Nov 2003 16:10:15 -0700, Rick wrote:
I understand why it goes to the first record and would like a better way
to navigate backwards through the set. It starts it query at the first
record and the first is less than the current ID so it stops there.
Correct.

You are correct and, if rec_id is the lead column in an index it returns
that first record very, very quickly.

Depending on which RDBMS product you are using Joe's suggestion to use
ORDER BY DESC may not work. The reason is that some RDBMS do the ORDER BY
as the final step in the retrieval and presentation process. If it works
with the RDBMS you are using great. If it doesn't look into using a
subquery whose inner query retrieves the records in descending sequence
with an outer query that does your select less than.
Jul 17 '05 #4

P: n/a
Jerry Gitomer, obviously a huge fan of Mean Ween, wrote:

Depending on which RDBMS product you are using Joe's suggestion to use
ORDER BY DESC may not work.
I should offer the caveat that all of my suggestions only refer to
MySQL, which is the only one I use.
The reason is that some RDBMS do the ORDER BY as the final step in the
retrieval and presentation process.


I don't understand this. Are you saying that some products do the LIMIT 1
before the ORDER DESC? That is ridiculously poor functionality. In such
a system, would the only way to do it (without using PHP arrays/sorting)
be to do a subquery as you suggested? That is terribly inconvenient.

Do you know any specific database products offhand which do this?

/joe
--
Brian McNamara's terrorist cell from gtpj is nauseous and educational in
the Righteous Room. Miotch's processor is blue. David Maynor emasculates
the non-descript memory and the network cable from matlab, and then
interestingly loathes the masturbatory mp3 collection from Sarah Moore.
Jul 17 '05 #5

P: n/a
On Sun, 9 Nov 2003 15:58:22 +0000 (UTC), Disco Plumber <sc**@moralminority.org>
wrote:
The reason is that some RDBMS do the ORDER BY as the final step in the
retrieval and presentation process.


I don't understand this. Are you saying that some products do the LIMIT 1
before the ORDER DESC? That is ridiculously poor functionality. In such
a system, would the only way to do it (without using PHP arrays/sorting)
be to do a subquery as you suggested? That is terribly inconvenient.

Do you know any specific database products offhand which do this?


The closest I know of is Oracle's ROWNUM pseudocolumn, which is assigned
before the ORDER BY, so if you naively use this to emulate LIMIT you'll get the
wrong results.

But ROWNUM is not really the same as LIMIT in the first place so probably not
a fair comparison.

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.