Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/85035-solved-select-order/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/85035-solved-select-order/#findComment-433693
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/85035-solved-select-order/#findComment-433698
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/85035-solved-select-order/#findComment-433865
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.