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! Link to comment https://forums.phpfreaks.com/topic/59002-solved-previous-database-id/ 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 Link to comment https://forums.phpfreaks.com/topic/59002-solved-previous-database-id/#findComment-292834 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? Link to comment https://forums.phpfreaks.com/topic/59002-solved-previous-database-id/#findComment-292838 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. Link to comment https://forums.phpfreaks.com/topic/59002-solved-previous-database-id/#findComment-292840 Share on other sites More sharing options...
cooldude832 Posted July 9, 2007 Share Posted July 9, 2007 mark it solved please Link to comment https://forums.phpfreaks.com/topic/59002-solved-previous-database-id/#findComment-292845 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. Link to comment https://forums.phpfreaks.com/topic/59002-solved-previous-database-id/#findComment-292854 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? Link to comment https://forums.phpfreaks.com/topic/59002-solved-previous-database-id/#findComment-292857 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! Link to comment https://forums.phpfreaks.com/topic/59002-solved-previous-database-id/#findComment-292880 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. Link to comment https://forums.phpfreaks.com/topic/59002-solved-previous-database-id/#findComment-292885 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) Link to comment https://forums.phpfreaks.com/topic/59002-solved-previous-database-id/#findComment-292890 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??? Link to comment https://forums.phpfreaks.com/topic/59002-solved-previous-database-id/#findComment-292902 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... Link to comment https://forums.phpfreaks.com/topic/59002-solved-previous-database-id/#findComment-292908 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. Link to comment https://forums.phpfreaks.com/topic/59002-solved-previous-database-id/#findComment-292928 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 Link to comment https://forums.phpfreaks.com/topic/59002-solved-previous-database-id/#findComment-292972 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.