GrayFox12 Posted September 13, 2007 Share Posted September 13, 2007 Hey i am trying to get the previous record using a query. At the moment i get the next record by using $query0 = "SELECT * FROM SL_Register WHERE IDNumber > '$IDN'"; which works fine, so naturally i put the opposite to get the previous record. $query0 = "SELECT * FROM SL_Register WHERE IDNumber < '$IDN'"; But this only returns the first record in the table, whereas i want the record that is 1 below the current record, $IDN. Please help Quote Link to comment https://forums.phpfreaks.com/topic/69137-solved-get-previous-record/ Share on other sites More sharing options...
Jessica Posted September 13, 2007 Share Posted September 13, 2007 WHERE IDNumber = ($ISN-1) Quote Link to comment https://forums.phpfreaks.com/topic/69137-solved-get-previous-record/#findComment-347519 Share on other sites More sharing options...
GrayFox12 Posted September 14, 2007 Author Share Posted September 14, 2007 That will not work. Here is example of an IDNumber. 'E1003'. Quote Link to comment https://forums.phpfreaks.com/topic/69137-solved-get-previous-record/#findComment-348043 Share on other sites More sharing options...
teng84 Posted September 14, 2007 Share Posted September 14, 2007 what is the data type of your field if that is a char then thats a prob Quote Link to comment https://forums.phpfreaks.com/topic/69137-solved-get-previous-record/#findComment-348048 Share on other sites More sharing options...
Jessica Posted September 14, 2007 Share Posted September 14, 2007 Would the one before it be E1002? You could just remove the e, subtract one in php and add it back on. Otherwise, you'll have to use an actual ID number as in a unique id field in your table. Quote Link to comment https://forums.phpfreaks.com/topic/69137-solved-get-previous-record/#findComment-348050 Share on other sites More sharing options...
GrayFox12 Posted September 14, 2007 Author Share Posted September 14, 2007 Yes its varchar. Um how would i go about subtracting a character off? You sure there isnt a function in MSSQL i could use. Quote Link to comment https://forums.phpfreaks.com/topic/69137-solved-get-previous-record/#findComment-348057 Share on other sites More sharing options...
teng84 Posted September 14, 2007 Share Posted September 14, 2007 I would suggest change the id to int for auto increment you cannot use order by clause to obtain what you need because it will treat your field as char not numbers Yes its varchar. Um how would i go about subtracting a character off? You sure there isnt a function in MSSQL i could use. this idea will waste your time and slow your code although this will work Quote Link to comment https://forums.phpfreaks.com/topic/69137-solved-get-previous-record/#findComment-348058 Share on other sites More sharing options...
Jessica Posted September 14, 2007 Share Posted September 14, 2007 use the string functions in php, such as substr. The best thing to do would be to use a regular ID, not an alphanumberic one. Quote Link to comment https://forums.phpfreaks.com/topic/69137-solved-get-previous-record/#findComment-348060 Share on other sites More sharing options...
teng84 Posted September 14, 2007 Share Posted September 14, 2007 you have an option option 1 : do it this way and do string manipulation and use doubled query and have more or less 20 lines option 2: change your field to int and auto increment it and query once and have a single line for this stuff Quote Link to comment https://forums.phpfreaks.com/topic/69137-solved-get-previous-record/#findComment-348063 Share on other sites More sharing options...
GrayFox12 Posted September 14, 2007 Author Share Posted September 14, 2007 Hang on i just tried this query which worked, but im not sure how get all select all the other columns $query0 = "SELECT max(IDNumber) FROM SL_Register WHERE IDNumber < '$IDN'"; Quote Link to comment https://forums.phpfreaks.com/topic/69137-solved-get-previous-record/#findComment-348071 Share on other sites More sharing options...
Jessica Posted September 14, 2007 Share Posted September 14, 2007 I'm not sure max will work the way you want on a varchar field, but okay. After that just add the other fields. max(IDNumber), column2, column3 Quote Link to comment https://forums.phpfreaks.com/topic/69137-solved-get-previous-record/#findComment-348074 Share on other sites More sharing options...
GrayFox12 Posted September 14, 2007 Author Share Posted September 14, 2007 Yea it kinda works. In some cases it skips some records for some reason. Anyway thankyou for your help Quote Link to comment https://forums.phpfreaks.com/topic/69137-solved-get-previous-record/#findComment-348082 Share on other sites More sharing options...
redarrow Posted September 14, 2007 Share Posted September 14, 2007 what about this $query0 = "SELECT * FROM SL_Register WHERE IDNumber < SUBSTRING('$IDN',1)-1"; got no idear does this work?????? Quote Link to comment https://forums.phpfreaks.com/topic/69137-solved-get-previous-record/#findComment-348083 Share on other sites More sharing options...
GrayFox12 Posted September 14, 2007 Author Share Posted September 14, 2007 Wicked thx mate that worked. i used it this way $id = SUBSTR($IDN ,1,4)-1; $query0 = "SELECT * FROM SL_Register WHERE IDNumber LIKE '%$id%'"; Quote Link to comment https://forums.phpfreaks.com/topic/69137-solved-get-previous-record/#findComment-348098 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.