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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

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 ;-)
Roger BaklundCommented:
An even simpler solution:
select min(id) from mytable
select max(id) from mytable

Open in new window

PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

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

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

Computer101Commented:
PAQed with points refunded (500)

Computer101
EE Admin

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.