refiking Posted April 24, 2008 Share Posted April 24, 2008 I am trying to retrieve the records in order by the did field. For some reason it retrieves the first one and then skips to the record with a did of 10. Here is my code: $sql = mysql_query("SELECT * FROM dregs WHERE lid = '$lid' AND sid = '$sid' AND tm = '' ORDER BY did LIMIT 3"); Quote Link to comment https://forums.phpfreaks.com/topic/102734-solved-db-records-retrieved-out-of-order/ Share on other sites More sharing options...
haku Posted April 24, 2008 Share Posted April 24, 2008 Can you do an order by without saying which direction to order it in? Quote Link to comment https://forums.phpfreaks.com/topic/102734-solved-db-records-retrieved-out-of-order/#findComment-526114 Share on other sites More sharing options...
p2grace Posted April 24, 2008 Share Posted April 24, 2008 Hmm not sure if this will work but it's worth a shot: <?php $sql = mysql_query("SELECT * FROM `dregs` WHERE `lid` = '$lid' AND `sid` = '$sid' AND `tm` = '' ORDER BY `did` LIMIT 0,3"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/102734-solved-db-records-retrieved-out-of-order/#findComment-526115 Share on other sites More sharing options...
p2grace Posted April 24, 2008 Share Posted April 24, 2008 To answer your question yes, it defaults to asc order. Quote Link to comment https://forums.phpfreaks.com/topic/102734-solved-db-records-retrieved-out-of-order/#findComment-526117 Share on other sites More sharing options...
refiking Posted April 24, 2008 Author Share Posted April 24, 2008 No. That didn't work, either. When I did the order by desc, it went 9, 8, 7 and there are 50 entries. I don't get it. It skips 2-9 on ASC and skips 50-10 on desc. Quote Link to comment https://forums.phpfreaks.com/topic/102734-solved-db-records-retrieved-out-of-order/#findComment-526135 Share on other sites More sharing options...
p2grace Posted April 24, 2008 Share Posted April 24, 2008 Try typing the query straight into phpmyadmin and see if it does the same thing Quote Link to comment https://forums.phpfreaks.com/topic/102734-solved-db-records-retrieved-out-of-order/#findComment-526139 Share on other sites More sharing options...
refiking Posted April 24, 2008 Author Share Posted April 24, 2008 Yes. It returned the same thing Quote Link to comment https://forums.phpfreaks.com/topic/102734-solved-db-records-retrieved-out-of-order/#findComment-526144 Share on other sites More sharing options...
p2grace Posted April 24, 2008 Share Posted April 24, 2008 Then it must be something with the data. The query is correct. Quote Link to comment https://forums.phpfreaks.com/topic/102734-solved-db-records-retrieved-out-of-order/#findComment-526150 Share on other sites More sharing options...
refiking Posted April 24, 2008 Author Share Posted April 24, 2008 The data has the correct did I just quadruple checked. There is a 1,2,3,4,5,6.........50 did record. Quote Link to comment https://forums.phpfreaks.com/topic/102734-solved-db-records-retrieved-out-of-order/#findComment-526153 Share on other sites More sharing options...
haku Posted April 24, 2008 Share Posted April 24, 2008 It sounds like the numbers may be saved in your database as text - if that is the case, then when you order it, it will order like this♦: 1 10 11 12 13 14 15 16 17 18 19 2 20 21 22 ...etc They will only order as 1,2,3,4,5 etc if they are saved as numbers. Quote Link to comment https://forums.phpfreaks.com/topic/102734-solved-db-records-retrieved-out-of-order/#findComment-526156 Share on other sites More sharing options...
p2grace Posted April 24, 2008 Share Posted April 24, 2008 What's the record type, is it integer? It makes sense for it to do that if it isn't integer, it's probably varchar. Quote Link to comment https://forums.phpfreaks.com/topic/102734-solved-db-records-retrieved-out-of-order/#findComment-526158 Share on other sites More sharing options...
p2grace Posted April 24, 2008 Share Posted April 24, 2008 Yeup haku is right, that's the only other explanation. Quote Link to comment https://forums.phpfreaks.com/topic/102734-solved-db-records-retrieved-out-of-order/#findComment-526159 Share on other sites More sharing options...
refiking Posted April 24, 2008 Author Share Posted April 24, 2008 That was it! It was varchar. Changed it and it works perfectly! Quote Link to comment https://forums.phpfreaks.com/topic/102734-solved-db-records-retrieved-out-of-order/#findComment-526168 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.