Re: Retrieving Rows - Can I Change The Order?

From: Date: Thu, 26 Jul 2001 07:11:14 +0000
Subject: Re: Retrieving Rows - Can I Change The Order?
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-10788@lists.php.net to get a copy of this message
on 7/25/01 11:46 PM, Dave Watkinson at d.watkinson@mightymatch.com wrote: > By the way, I know I can be explicit by SELECTing each column by name, but > I'ds rather use a SELECT * fror this partucular instance. That would be better, of course. :) Another thing you can do is a mysql_fetch_array() and then reference by column name which probably won't change as often as the column number. That is, instead of $row[4] you ask for $row['my_column_name']. For a specific solution, you'll find one in the MySQL manual and it's probably applicable to other RDBMS. <START QUOTE> How to change the order of columns in a table ============================================= The whole point of SQL is to abstract the application from the data storage format. You should always specify the order in wish you wish to retrieve your data. For example: SELECT col_name1, col_name2, col_name3 FROM tbl_name; will return columns in the order col_name1', col_name2', `col_name3', whereas: SELECT col_name1, col_name3, col_name2 FROM tbl_name; will return columns in the order col_name1', col_name3', `col_name2'. You should *NEVER*, in an application, use `SELECT *' and retrieve the columns based on their position, because the order in which columns are returned *CANNOT* be guaranteed over time; A simple change to your database may cause your application to fail rather dramatically. If you want to change the order of columns anyway, you can do it as follows: 1. Create a new table with the columns in the right order. 2. Execute `INSERT INTO new_table SELECT fields-in-new_table-order FROM old_table'. 3. Drop or rename `old_table' 4. `ALTER TABLE new_table RENAME old_table' <END QUOTE> Hope it helps. Sincerely, Paul Burney ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Paul Burney Webmaster && Open Source Developer UCLA -> GSE&IS -> ETU (310) 825-8365 <webmaster@gseis.ucla.edu> <http://www.gseis.ucla.edu/> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

« previous php.db (#10788) next »