[thelist] Rusty PHP questions...

Luther, Ron Ron.Luther at hp.com
Tue Oct 16 13:08:39 CDT 2007

Joel D Canfield noted:

> 1. One best practice is to never use SELECT * - could someone explain 
> why, and tell me when this is okay if ever? Right now I am doing this:

>>my primary argument is future needs: listing all the columns makes it 
>>really easy to change from 'select * ' to 'select * except for column 
>>13' which you can't do. it's also self-documenting. right there in
>>code you've got all the column names, when you come back in six weeks 
>>to adjust something.

Hi Tom,

Let me build on Joel's good example.  I think many people view 'select
*' as lazy 
or as fast but somehow less than best practice -- but they don't see any
harm to it, right?  What could it hurt?   ;-)

My guess is that this is probably because they may have 'ownership' of
the entire 
data chain from creation to manipulation to loading and reporting.  They
everything about the data flow and approve or personally make any
changes that 
happen.  They have full control.  That's nice.  But that's not always
the case.

Sometimes you are not the only developer working off that data store.
you are not the only person or the only group who can affect a change to
table structure.  That is the case where 'select *' gets scary, and why
it's a 
good idea to give up the practice now.  It doesn't "scale" to working on
projects with bigger teams ... for potentially bigger money ;-).

* Now, right off the bat, if your design depended on a fixed number of
or on a specific field structure (e.g. "name" is field # 3 or the total
of fields is divisible by four) then any structural table change has the
to completely flummox your design and break your app ... And pretty much
trash years of cumulative work.  Fun Stuff!

* Granted, fields are often added at the end of the table, which can
still wreck 
your design, but usually not as badly.  But rework is always fun, right?

So let's look at a different issue.

* Those newly added db fields may contain confidential information that
not be made available to the audience you are 'select *'-ing to.
Consider an 
extra-net application made available to your suppliers and large
customers.  If 
some dba in another part of the company thinks adding supplier credit
ratings to 
this table is a good idea, then _your_ [select * --> display *]
(perhaps written years before the table change) may suddenly expose the
to legal liabilities ... which is not a conversation you want to be on
the receiving 
end of!

There are a lot of areas; HR applications, Payroll applications, Finance
where it may turn out to be MUCH more than merely a bad idea to 'select

It's safest to explicitly call out the fields you intend to use.  [More
readable and 
better documentation too!]  Select * is more than just lazy - it can get
you and the 
company into hot water.


More information about the thelist mailing list