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
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!

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.