Record Column Order and SELECT *

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Gleeb
Forum Commoner
Posts: 87
Joined: Tue May 13, 2003 7:01 am
Location: UK
Contact:

Record Column Order and SELECT *

Post by Gleeb »

Just a quick question, I could probably find out using google or something, but, I intend to use an MD5 hash of a table to check someone's cookie is valid (yes, I'm using othermethods too).

'SELECT * from `table`;' - would the order of columns change at all? I.E. Appear in an unpredetermined order? I should probably sort the array by keyname or something, to ensure that the order is consistant... or randomise the order once a day (to reduce ease of cracking)...

Anyway, I hope you can figure out what I'm asking ;)
User avatar
releasedj
Forum Contributor
Posts: 105
Joined: Tue Jun 17, 2003 6:35 am

Post by releasedj »

Presuming you rusing MySQL: The columns in a table remain the same unless there's an ALTER TABLE statement issued that changes it.
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

key

Post by phpScott »

If you have a key field on the table with maybe an autoIncrement or a unique key the just add a ORBER BY clause to your sql and you will always get the same order with any new additions on the end.

I know I have had to do this several times because the order had to remain constant.

phpScott
User avatar
releasedj
Forum Contributor
Posts: 105
Joined: Tue Jun 17, 2003 6:35 am

Post by releasedj »

I think Gleeb was talking about the order of the columns not the rows.
fractalvibes
Forum Contributor
Posts: 335
Joined: Thu Sep 26, 2002 6:14 pm
Location: Waco, Texas

Post by fractalvibes »

I think you are always better off specifying the columns you want rather than select * - a good habit to get into.

select colM
, colR
, colB
, colD
from sometable

guarantees that you get the columns in the order you specify. If the resulting rows need to be in a specific order, add an Order By clause
as you need.

Not sure about MySQL, but most databases have some sort of catalog tables that contain metadata about the database and you can find out the physical order of how the columns are stored in each row in a table...

Phil J.
Gleeb
Forum Commoner
Posts: 87
Joined: Tue May 13, 2003 7:01 am
Location: UK
Contact:

Post by Gleeb »

Thanks for your help :)
Post Reply