Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

Last row in a table

Codes here !

Moderators: macek, egami, gesf

Last row in a table

Postby Xerpher » Sun Sep 01, 2002 8:08 pm

How would be the best way to fetch the last row in a MySQL table?
Xerpher
New php-forum User
New php-forum User
 
Posts: 164
Joined: Tue Aug 27, 2002 8:25 pm
Location: Ontario, Canada

Postby DoppyNL » Mon Sep 02, 2002 12:57 am

I can find only one reason why you want the last row, and that's to find out wich id was added with the last insert query.
so, I think you're looking for this:
Code: Select all
$inserted_id = mysql_insert_id($query_result);


mysql doesn't sort the table by itself, so getting the last record from it is almost completely random.

Greetz Daan
DoppyNL
 

Re: Last row in a table

Postby Jay » Mon Sep 02, 2002 10:45 am

Xerpher wrote:How would be the best way to fetch the last row in a MySQL table?

That would depend on how your table is sorted too! Just select one row and sort the 'sort' column in descending order!

If if you're after the last row literally, you could count how many rows there are and then use (at the end of the query)
limit (totalRows-1),1

where totalRows is how many rows there are, and 1 is how many rows it'll display after that!
Jay
 

Postby serge » Fri Sep 20, 2002 7:41 am

DoppyNL wrote:.....
mysql doesn't sort the table by itself, so getting the last record from it is almost completely random.
Greetz Daan


8) if there is no "ORDER" condition in query, mysql returns recordset in order it stored in database. So:
=============================
while ($db_obj=mysql_fetch_object($result))
{
$last_element=$db_obj->some_field;
}
=============================
you have the some_field from the last row!

------------------------
With best regards,
Serge Bulanov
AlarIT Web developer,
http://www.alarit.com
serge
New php-forum User
New php-forum User
 
Posts: 14
Joined: Mon Sep 16, 2002 1:25 am
Location: Ukraine

Postby DoppyNL » Fri Sep 20, 2002 7:49 am

serge wrote:
DoppyNL wrote:.....
mysql doesn't sort the table by itself, so getting the last record from it is almost completely random.
Greetz Daan


8) if there is no "ORDER" condition in query, mysql returns recordset in order it stored in database.


mysql DOES NOT return the recordset the way it was entered in the database!!!!
when you haven't deleted any records you may get what you expect, but when you deleted some rows and/or are working with records of a variable length and insert some after that you WILL get some other order wich is NOT the order you entered the records!!

Greetz Daan
DoppyNL
 

Postby serge » Fri Sep 20, 2002 9:23 am

DoppyNL wrote:
serge wrote:
DoppyNL wrote:.....
mysql doesn't sort the table by itself, so getting the last record from it is almost completely random.
Greetz Daan


8) if there is no "ORDER" condition in query, mysql returns recordset in order it stored in database.


mysql DOES NOT return the recordset the way it was entered in the database!!!!
when you haven't deleted any records you may get what you expect, but when you deleted some rows and/or are working with records of a variable length and insert some after that you WILL get some other order wich is NOT the order you entered the records!!

Greetz Daan

:twisted: But it is order as records stored in database!
The question was:
How would be the best way to fetch the last row in a MySQL table?

------------------------
With best regards,
Serge Bulanov
AlarIT Web developer,
www.AlarIT.com
Last edited by serge on Fri Sep 20, 2002 9:27 am, edited 1 time in total.
serge
New php-forum User
New php-forum User
 
Posts: 14
Joined: Mon Sep 16, 2002 1:25 am
Location: Ukraine

Postby DoppyNL » Fri Sep 20, 2002 9:26 am

serge wrote: :twisted: But it is order as records stored in database!

That's true, but you don't know what order that is :D :mrgreen: :!:
DoppyNL
 

Postby serge » Fri Sep 20, 2002 9:30 am

I repeat:
The task is to find LAST ROW
:!:
------------------------
With best regards,
Serge Bulanov
AlarIT Web developer,
www.AlarIT.com
serge
New php-forum User
New php-forum User
 
Posts: 14
Joined: Mon Sep 16, 2002 1:25 am
Location: Ukraine


Return to mySQL & php coding

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.