studgate Posted July 9, 2007 Share Posted July 9, 2007 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! Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted July 9, 2007 Share Posted July 9, 2007 http://us.php.net/manual/en/function.mysql-insert-id.php Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 9, 2007 Share Posted July 9, 2007 mysql_insert_id() will return the ID of the previously INSERTED id and I'm not sure it's suitable for this case. I think you'll need to run a sELCT query on Jean to get her ID then subtract one - is that what you mean? Quote Link to comment Share on other sites More sharing options...
studgate Posted July 9, 2007 Author Share Posted July 9, 2007 Yesideez, this is exactly what I want. Please help, Thank again. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted July 9, 2007 Share Posted July 9, 2007 mark it solved please Quote Link to comment Share on other sites More sharing options...
studgate Posted July 9, 2007 Author Share Posted July 9, 2007 It is not solved! How do I get the previous id for any data?? How do you get the previous id for the current ID?? Thanks again. Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 9, 2007 Share Posted July 9, 2007 <?php $fetch=mysql_fetch_assoc(mysql_query("SELECT `id` FROM `table` WHERE `name`='Jean'")); $id=$fetch['id'] $id--; echo 'ID before Jean is '.$id; ?> That what you mean? Quote Link to comment Share on other sites More sharing options...
studgate Posted July 9, 2007 Author Share Posted July 9, 2007 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! Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 9, 2007 Share Posted July 9, 2007 That isn't possible - you have to have a place in the table to reference as it doesn't behave like file access does. With a file if you open it and read x bytes into it the pointer will stay at x bytes in. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted July 9, 2007 Share Posted July 9, 2007 this is because mysql tables are not so linear in their file construction, indexing optimizes the tables by moving more oftenly used data to easier accessed parts of the file (i.e beginning) Quote Link to comment Share on other sites More sharing options...
studgate Posted July 9, 2007 Author Share Posted July 9, 2007 So there is no way to do something like this $jean_id= X; previd=SELECT * FROM $wpdb->posts WHERE ID={$jean_id->ID} So I have to know the id for Jean to get the previous ID and the name??? Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 9, 2007 Share Posted July 9, 2007 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... Quote Link to comment Share on other sites More sharing options...
studgate Posted July 9, 2007 Author Share Posted July 9, 2007 Thank everyone who replied. I could not find a solution and I want to put the topic as solved to close it. Thanks again. Quote Link to comment Share on other sites More sharing options...
AndyB Posted July 9, 2007 Share Posted July 9, 2007 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.