PHP lessons learned

I did a little more work on the WREK web pages today. I do believe I’m
to the point where I can bat out PHP pages that do a number of DB
tasks pretty simply. I’m at the point where I have the hard won
knowledge. Here are a few of my nuggest that I’ve learned, all the
hard way.

– At least for Oracle and mysql, use the FetchInto type functions and
get your results as associative arrays. There is a runtime hit, but it
makes your code much much more readable than cryptic index based
functions like “OciResult(, 3)” that force you to count columns in
your original SQL statement to see what the hell is going on.

– If you are getting a number of results to present as a table or
list, save them off into an array of the arrays from above. This
allows you to do your own sorting which might be different from the
database’s “order by”. It also enables cool things like
allowing people to sort differently via HTML forms with minimal

– Create one or a set of files you include in standard pages. If you
have a utility type function that you find yourself cutting and
pasting into another page, don’t. Put it in the utility include
instead. Otherwise, you’ll one day find yourself fixing a bug and then
having to grep to find all the files you put that bug in. Past it in
the include and remove it from all your PHP pages.

– Use source control, even for your own trivial projects at home and
even if you are the only person touching the files. There is nothing
like having that history to fall back on, and just being able to avoid
having all those “file.old”, “file.backup”, and crap like that is
worth it. I’m on the cusp of installing Subversion to try it out.