Overriding PHP.INI

It’s a while since I’ve needed to override the php.ini file on a per-site basis, and I hadn’t realised that since PHP 5.3, the process has changed slightly. Now, if you want a local ini file to be read, you need to name it .user.ini (note the dot at the beginning of the filename), and not php.ini. I couldn’t understand why my local php.ini was being ignored.

As before, place your .user.ini file in the directory within which you want your overrides to apply. The syntax is exactly the same as in the php.ini file itself. The manual doesn’t make this terribly clear; see the user-contributed notes.

Posted in PHP, Web development | Leave a comment

MySQL: updating one table from another

I always find this a bit awkward, especially if there isn’t a straightforward (i.e. primary key) way of matching records across tables. I find the INNER JOIN syntax easier here — I always do a SELECT using the join criteria first, and inspect the results to make sure the right rows are getting matched, before doing an update.

UPDATE Property1 PI INNER JOIN Property2 TP ON
PI.column1=TP.column1 AND PI.column2=TP.propid
SET PI.seqno = TP.seqno
WHERE PI.recordtype=4 AND PI.seqno IS NULL

Note that I’m only updating one of the tables here, the one specified immediately after the UPDATE keyword.

Posted in Databases | Leave a comment

Timing it right

Apparently Americans have trouble with times expressed in 24-hour format and keep turning up at the wrong time as a result. Here’s yet another remedy for ludicrously over-complicated solutions to a simple date/time problem:

$time = date("g:i A", strtotime("17:30:00"));

That will give you 5:30 PM. If you want a lower-case am/pm, replace the upper-case A with a lower-case one.

Posted in PHP | Leave a comment

Cherry pickin’

Oops. Made a change in the wrong branch of my git repository. Of course I knew there had to be an easy way of merging that one commit into the right branch, and there is. How to do it in Tower. And how to do it at the command line (skip down to “Rebasing and Cherry Picking Workflows”).

Posted in Web development | Tagged | Leave a comment

Things I didn’t know about MySQL #1: GREATEST

Somehow I have used SQL for nearly 30 years without knowing about the GREATEST function. It’s only when you need to do something that looks impossible that you delve into the online manual and find gems like this.

In this case, I had a query that needed to return, and order the rows by, the latest date from three different date columns in three different tables. Impossible I thought — I’ll have to muck about with MAX for each column, and then do the comparison in PHP. Not at all!

SELECT R.requestid, C.cliid, clifname, clilname,requestdate, GREATEST(emaildate,climoddate,requestlastmodified) AS lastmoddate
FROM ClientRequests R
INNER JOIN Clients C ON C.cliid = R.cliid
LEFT OUTER JOIN Emails E ON R.requestid = E.requestid
GROUP BY R.requestid
ORDER BY GREATEST(emaildate,climoddate,requestlastmodified)

If one or more of the columns might include null values, you need to do a little more work with COALESCE:
SELECT R.requestid, C.cliid, clifname, clilname,requestdate, GREATEST(COALESCE(emaildate,0),climoddate,requestlastmodified) AS lastmoddate
FROM ClientRequests R
INNER JOIN Clients C ON C.cliid = R.cliid
LEFT OUTER JOIN Emails E ON R.requestid = E.requestid
GROUP BY R.requestid
ORDER BY GREATEST(COALESCE(emaildate,0),climoddate,requestlastmodified)

Posted in Databases | Leave a comment

User-serviceable parts inside

Screwdrivers needed for MacBook upgrade

Apple seem to have developed a reputation for hardware that can’t be upgraded by the user, either because it’s physically impossible, it voids the warranty, or both. However I can report success with my aged white MacBook. This isn’t a “how to upgrade your Mac” article, because different models differ radically in how to upgrade: if you’re thinking of doing this, I highly recommend searching YouTube for your model and watching one of the many videos showing how to do it — that’s the best way of judging how difficult/dangerous it’s likely to be.

Originally bought as a “travelling only” laptop in early 2009, when my main computer was a desktop PC, the MacBook has been my main workhorse for the last three years. I upgraded the memory to the maximum 4GB when I bought it, but it only had a 120 GB hard disk. Fast forward to mid-2013 and it is labouring along with less than 5% free disk space. Everything takes an age; new tabs in Chrome take seconds to appear, and if I want to crank up OpenOffice I might as well go and have a coffee while I wait. Even using a simple text editor, I found myself waiting for the computer to respond to key presses or mouse clicks.

I dare say some people would say this computer is too old to be worth upgrading — why not just replace it? But I’m used to keeping my hardware for at least five years — in fact I still have a Dell tower PC that I bought in 1999. The only option given that it’s a laptop seemed to be upgrading to a faster, larger hard disk, possibly a solid state one (SSD). These are what Apple puts in the MacBook Air — a lot more expensive per GB than conventional disks, but much faster because they have no moving parts. I liked the sound of them, but after further research I plumped for a hybrid disk from Seagate: a 750 GB Momentus XT. The cost per GB is much less than for SSDs, but it uses a solid-state cache to achieve SSD-like performance. I’m told that as you use it, it learns which files are used most often and caches them for quick access, so it should get faster over time.

I was a bit nervous about doing open-heart surgery on the Mac, but Amazon reviews and a reassuring YouTube video convinced me to take the plunge — as well as alerting me to the fact that I needed to buy a cheap set of Torx screwdrivers. It turned out to be an easy 10-minute job; I minimised possible stress by covering my work surface with a clean cloth and laying out all my tools (two screwdrivers and a 5-cent coin) before I started. In fact it’s the easiest disk replacement I’ve ever done — once you have the battery and the protective plate out, you can just slide the old disk out using the helpfully provided tab, swap the chassis onto the new one (this is what you need the Torx screwdriver for), and then slide the new one in.

What about data transfer? Also relatively painless; the night before, I used SuperDuper! to clone the hard disk onto a USB drive — a slow process that took about four hours — and then checked I could boot from the USB drive before I started the upgrade (on a Mac, holding down the alt key while booting lets you choose the boot drive). After the upgrade, I booted from the USB, formatted the new disk using Disk Utility, and cloned the old data back onto it. Holding my breath and turning my back so I didn’t have to watch, I rebooted from the new drive, and all was well!

Verdict: it was definitely a worthwhile upgrade to prolong the useful life of the MacBook at relatively low cost. It’s like using a real computer now. Applications are opening much more quickly, and even web browsing seems faster. Previously using anything resource-hungry or watching a video would start the fan whirring continuously; now I hardly hear it. The new hard disk makes a bit more noise than the old one, which was silent, but that’s a small price to pay for the higher speed. It seems that the nearly full hard disk was having a very serious effect on performance. With 640 GB free, I don’t think I’ll be facing that problem again for a while …

Posted in Hardware, Mac | Leave a comment

git: revert a single file to a specific revision

Turns out to be quite easy:

  1. Identify the hash of the revision you want to revert to, e.g. abc123
  2. At the command prompt, type:

git checkout abc123 path/to/file

Why would I need to do this? Don’t ask :) But I definitely took something to heart: git positively encourages you to create branches, and I don’t do it enough. If I had made a branch for the single feature I had a problem with, instead of rolling it into a general “development” branch, I wouldn’t have been at the computer at 9:30 pm tearing my hair out trying to roll back individual files! Lesson learned. But I do one thing right: atomic commits. Make sure each commit relates to a single feature, then at least it’s easy to identify affected files. Commits of unrelated fixes are a Bad Thing.

Posted in Web development | Tagged , , , | Leave a comment

I love you PHP: more date goodness

Want to display a date in a language other than English, without switching your whole installation to some other language? It’s so easy …

setlocale(LC_TIME, "fr_FR");
$datefacture = strftime("%e %B %Y");
echo $datefacture;

Posted in PHP | Tagged , | Leave a comment

PHP date calculations: last day of the month

PHP’s date() function must be my favourite PHP function. You need never struggle with manipulating dates in PHP — if you need to use more than one line of code for working out a date, you probably just haven’t looked at the date function closely enough. This one’s from here; it works out the last day of the previous month. Yes, even when it’s a leap February.
echo date("t/m/Y", strtotime("last month"));

Posted in PHP | Tagged | Leave a comment

jQuery validation plugin: required select elements

I’m just posting this as a reminder to myself, but it may help others! I found that when I added class=”required” to a select element in a form, it wasn’t actually treated as required by jQuery. After some fiddling about, I discovered that you can easily force it to be required by ensuring that you have a blank first option with a value of an empty string (zero will not do!). In other words:
<option value="" selected="selected"></option>
Note that I forced this option to be selected if I hadn’t passed a valid default option to be preselected when the form was loaded. This means that if the empty option is still selected (or has been chosen by the user) when the form is submitted, jQuery will detect it as required and display the appropriate error.

Posted in Javascript | Tagged , | Leave a comment