Jump to content

[SOLVED] Previous database ID


studgate

Recommended Posts

Hi Guys,

  is there a way to get the previous id of an object in

my database and other information related to the data in

that previous id??

For example if I have a customer name Jean and I want to

get the previous person information before Jean. if Jean

id is 5, I want to get name of person in 4 (previous id= id-1)

without knowing the id for jean(big database).

Please help.

Thanks in advance!

Link to comment
https://forums.phpfreaks.com/topic/59002-solved-previous-database-id/
Share on other sites

Thanks Yesideez but I was looking for something

more  like this

 

<?php
  $fetch=mysql_fetch_assoc(mysql_query("SELECT * FROM `table` WHERE id='currentid -1'"));
  $id=$fetch['id']
  $id--;
  echo 'ID before Jean is '.$id and the name from .$id is .$id[name];
?>

I hope you understand.

Thanks!

As long as yuo have some information of Jean which is unique to the record then you can use that data to access Jean's record. Once you have that you can look at the ID and go back one to access the data.

 

SELECT `id`-1 FROM `table` WHERE `name`='Jean'

 

Think that'd work...

There's no guarantee that the record 'before' Jean's exists - it might well have been deleted in some other operation.

 

Do this as a two-step process.

 

SELECT id from tablename WHERE username = 'Jean'

abstract the value of id from the query result

 

SELECT * from tablename WHERE id<'$id' ORDER by id DESC LIMIT 1

abstract the data for the single record returned

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.