Getting Last Record From a MySQL Query

How do you find out what the last record in a mysql table is?

I tried having it do a mysql_num_rows and then seeing if the ID is greater than the total but that does not work because some of my entries in my table have an id of say...503 but there's only 200 total rows.

I need to be able to know if the current selected row is the LAST record in the database so that I know whether not the NEXT id should be the FIRST id...and that leads me to another question...how would I tell what the FIRST id is for the first row in the table?

If you need further clarification please let me know.

Thanks
Jeff LongEntrepreneur Asked:
Who is Participating?
 
Computer101Commented:
PAQed with points refunded (500)

Computer101
EE Admin
0
 
Matthew KellyCommented:
Does the below example help any?

You should be able to tell by the index location the result being displayed is using: http://www.php.net/mysql_result

If this is not what you are looking for please explain your query/code farther.
$sql = "SELECT * FROM tbl";
$result = mysql_query($sql);
 
echo "First record: ".mysql_result($result,0,"col_name");
echo "Last record: ".mysql_result($result,mysql_num_rows($result)-1,"col_name");
 
for ( $i=0; $i<mysql_num_rows($result);$i++ )
{
if ( $i==0 ) { echo "First record: ".mysql_result($result,$i,"col_name"); }
else if ( $i=(mysql_num_rows($result)-1) ) { echo "Last record: ".mysql_result($result,$i,"col_name"); }
else { echo "Middle record number ".$i.": ".mysql_result($result,$i,"col_name"); }
}

Open in new window

0
 
Jeff LongEntrepreneur Author Commented:
Nah I figured it out but thanks.  You're doing it waaay to complicated ;-)

It's as simple as doing a query and displaying the records using ASC to get the FIRST entry and DESC to get the LAST ;-)
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Roger BaklundCommented:
An even simpler solution:
select min(id) from mytable
select max(id) from mytable

Open in new window

0
 
Roger BaklundCommented:
...you could even combine these in the same query:
select min(id) as smallest,max(id) as largest from mytable

Open in new window

0
 
Marck911Commented:
Hi there, well with this function you can get it, and for instance: i am saving it into a ssession variable, the function must be placed after the mysql_query, about the first record you already answered, Xd, cheers!
query code
.
.
.
$Result1 = mysql_query($insertSQL, $ConextoDB) or die(mysql_error());
 
$_SESSION['last_focus_on_dbtable']=mysql_insert_id();

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.