Porting old ASP/Access code to PHP/MySQL06 Jan 2013
I've been trying to rationalise my websites a bit recently, and in particular finally put into mothballs what was probably my first website.
The site was first written in about 1999(!), and some of the code hasn't changed very much since then. The only part that is still really being used is a football prediction game called "Fantasy Island" - like fantasy football but a bit crap :-)
For me this was an interesting exercise in brushing up on my extremely rusty PHP skills, learning some new things about MySQL and looking back at some very old code. In particular there are a couple of things of note ...
- I've learnt a lot about coding in the last 14 years, as some of the old code is pretty ropey. In particular the way I (and hopefully everybody) think more about potential security issues is definitely a good thing.
- PHP is surprisingly similar to ASP Classic
The 2nd point is quite interesting, in particular given the slightly dismissive attitude that I've heard about PHP sometimes from people who were quite happy to develop in a very similar way not that long ago. Clearly it is easy to write "spaghetti code" in PHP and ASP, but unstructured code isn't a feature of any language, but more a function of the developer writing the code.
You can see the results of my handywork at http://www.yeltz.co.uk/fantasyisland/, and once I've done a final tidy up and fixed any bugs found now it's live I may share the code somewhere.
There were a couple of gotchas that tripped me up as I was doing the migration that I'll share here in case someone else hits them too.
DateTime->add() is not supported in PHP 5.2
I wanted to use a DateTime object to find a day 60 days from now, and on my dev box I was running PHP 5.4, so using the add() function seemed logical.
However what I didn't realise until later was that the server I've ported the code to is only running PHP 5.2.17, and a close look at the documentation showed the add() function was only supported in 5.3.0 and above.
The solution was to use the pretty self-explanatory following code:
$twoMonthsInterval = new DateTime(); $twoMonthsInterval->modify('+60 day');
Not as clean, but it works :-)
Use TINYINT(1) for boolean fields in MySQL
I had all sorts of issues which to be honest I didn't really understand when trying to retrieve data from a table that I'd set as a BOOL field in the database.
According to the documentation it's just a synonym for a TINYINT(1) field but when fetching the info via PDO I was getting incorrect values for some reason.
Anyway, directly setting the value to TINYINT(1) and setting/getting the 0 or 1 values directly fixed all of my issues.