revraz Posted January 8, 2008 Share Posted January 8, 2008 If you do a SELECT * on a table, will the resulting order always be the same? I thought I read somewhere where the order could be different each time if you don't specify it, but everytime I do it, it comes out the same. Is that because I use PHP mysql functions? Quote Link to comment https://forums.phpfreaks.com/topic/85035-solved-select-order/ Share on other sites More sharing options...
fenway Posted January 8, 2008 Share Posted January 8, 2008 I'm not sure what you mean by "the same"... if you use mysql_fetch_assoc(), you're asking for a hash, so the order is undefined (or should be). If you use mysql_fetch_array(), you get them back "in order"... but this means different things depending on the number of tables you have, the type of join, the join conditions (ON vs USING), etc. In any case, you shouldn't *EVER* base anything on this order at all... and moreover, there can't possible be any need to. Quote Link to comment https://forums.phpfreaks.com/topic/85035-solved-select-order/#findComment-433693 Share on other sites More sharing options...
revraz Posted January 8, 2008 Author Share Posted January 8, 2008 Example, table like id | name If I do a mysql_fetch_array, $row[0] returns id and $row[1] returns name. The book I read said that the result may be returned in a different order each time, maybe they meant the id order and not the field order. So in this case, $row[0] will always be id and $row[1] will always be name correct? Quote Link to comment https://forums.phpfreaks.com/topic/85035-solved-select-order/#findComment-433698 Share on other sites More sharing options...
revraz Posted January 8, 2008 Author Share Posted January 8, 2008 I think I misread, it was probably saying the resulting row order could vary, but the field order will be returned as displayed in the table layout. Quote Link to comment https://forums.phpfreaks.com/topic/85035-solved-select-order/#findComment-433722 Share on other sites More sharing options...
fenway Posted January 8, 2008 Share Posted January 8, 2008 Example, table like id | name If I do a mysql_fetch_array, $row[0] returns id and $row[1] returns name. The book I read said that the result may be returned in a different order each time, maybe they meant the id order and not the field order. So in this case, $row[0] will always be id and $row[1] will always be name correct? You shouldn't ever use array indicies to find your columns... use the name of the column by asking for a hash. Besides, if you ask from "SELECT name, id FROM...", you'd get them reversed!! I think I misread, it was probably saying the resulting row order could vary, but the field order will be returned as displayed in the table layout. That makes more sense... yes, SELECT without ORDER BY will return the rows in arbitrary order -- if it looks "ordered", that's because (a) you're using an auto-increment field AND b) you haven't deleted and then inserted new records after optimizing the table. It's not robust -- if you want or expect an order, use an explicit ORDER BY clause. Quote Link to comment https://forums.phpfreaks.com/topic/85035-solved-select-order/#findComment-433865 Share on other sites More sharing options...
revraz Posted January 8, 2008 Author Share Posted January 8, 2008 Thanks for the tips, I think I'll switch to Objects. I was worried that if I add a field later on then I would have to redo my code because the order changed on the field layout. Quote Link to comment https://forums.phpfreaks.com/topic/85035-solved-select-order/#findComment-433876 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.