How do I display only the last row in a SQL db result?

Codes here !

Moderators: egami, macek, gesf

Post Reply
User avatar
mammal
New php-forum User
New php-forum User
Posts: 37
Joined: Wed Apr 02, 2003 1:31 am
Location: Hythe, UK

Fri Apr 04, 2003 12:35 am

This might be a complicated way of doing this but you could do two SQL queries. One to find the last unique id and then the next to search for the rest of the details under that ID number:

<?

// Replace the word "unique_id" with whatever your id label is for that column.
$query = "SELECT MAX(unique_id) FROM newsletters LIMIT 1";
$result = mysql_query ($query);
$row = mysql_fetch_array ($result);

$query2 = "SELECT * FROM newsletters WHERE unique_id = '$row[unique_id]'";
$result2 = mysql_query ($query2);

?>

There may be a way of doing it using only one query but hey, this will work.

User avatar
mammal
New php-forum User
New php-forum User
Posts: 37
Joined: Wed Apr 02, 2003 1:31 am
Location: Hythe, UK

Fri Apr 04, 2003 1:47 am

No worries...

User avatar
Redcircle
Moderator
Moderator
Posts: 826
Joined: Tue Jan 21, 2003 10:42 pm
Location: Michigan USA
Contact:

Fri Apr 04, 2003 6:28 am

if you are using auto_increment right after an insert you can get the last inserted id by using

$id = mysql_insert_id();

then you will need to make a new query and use $id

Post Reply
  • Information
  • Who is online

    Users browsing this forum: No registered users and 2 guests