Archive for June, 2007

1 + 1 = 3

Saturday, June 30th, 2007

or

the curious Firefox double page request

One of my current web-related projects involves a simple page counter. A page counter is really a very simple thing: each time a specific page is requested by a client, the page count should increase by one. The stateless nature of the web means that we need to persist this page count value somewhere – it could be a simple flat file, or it could be a database – whatever suits your needs. Whatever you choose, it’s really not that hard: 1 + 1 = 2 + 1 = 3 + 1 = 4 etc…

I was pretty upset to find that my page counter (which I wrote a few months ago and was working correctly) had suddenly started incrementing by two for each page request… WTF??? I scoured the code for any instances where I was updating the “PageViews” field in my table (there was only one, and it looked like it was only adding one each time). I checked that I hadn’t accidentally executed that SQL twice (nope – nothing had changed there and it all looked normal). I started wondering if, after too much red wine, I’d mistakenly coded a database trigger to auto-increment the page views somehow… but nope…

After fruitlessly wading through my code for several hours, I started typing random words into Google hoping to find the answer. I quickly stumbled upon this. My thanks go out to Mr Brian Pontarelli, as he has spent many more hours than I tracking down the source of the bug… Yup – the problem is that Firefox will request a page twice if your HTML markup contains an img tag with an empty src attribute. I’ve been reading through the comments on Mr Pontarelli’s blog, and a lot of people were asking why you’d have an empty src tag for an image in the first place. I was dynamically assigning the value using javascript when the page had loaded, and therefore the double-load problem was occurring.

Just another one of those little gotchas you’ve have to look out for as a developer I guess…

Random Sort in SQL

Monday, June 25th, 2007

A couple of days ago a colleague asked if it was possible to sort the results of an SQL query randomly. Whilst there isn’t a magic “SORT BY RANDOM” clause for MySQL we can use, there is one simple trick that does the job nicely:

SELECT RAND(NOW()), Field_1[, Field_N...] FROM Table ORDER BY 1

In addition to selecting whatever fields we want, we include a random integer (in this case, seeded with the current time) as the first column in the select list. The ORDER BY 1 clause specifies that we want the SQL engine to order by the first column in the result set, thus producing the desired result.

The above works well for MySQL, but for SQL Server things are a little different. SQLServer evaluates the RAND function only once, and we therefore need something a little more subtle. The Tek-Tips website has the perfect answer:

SELECT Field_1[, Field_N...] FROM Table ORDER BY NEWID()

SQLServer’s NEWID() function generates a new, random unique identifier, which this time is evaluated on every row, and gives us an easy way to generate the random sort order.

Marvin’s Last Days

Tuesday, June 5th, 2007

Or: Mushrooms Days 42, 43 and 44

Well, after a slow start, it’s all been happening over the last week or so. Marvin has grown at an enormous pace, and getting almost too big for his box. Here’s the photos from day 42:

Marvin Day 42 (a) Marvin Day 42 (b)

Over the last 4 days Marvin must have doubled in size – here’s day 43:

Marvin Day 43 (a) Marvin Day 43 (b)

Today, on day 44, I pulled Marvin from his box, and took his final few photos:

Fat Bastard Marvin Marvin Aerial View Marvin and the Sellotape

And that, my friends, is the end of the Mushroom Diaries. If you’ve enjoyed this fungal extravaganza, and would like a personally signed Marvin photo, please drop me a line, and I’ll see what I can arrange (there may be a small fee for postage and handling). Be quick though, as this limited time offer will end as soon as Marvin decomposes.

Thanks to all 3 of my readers for sticking with me through all this…

Mushroom Fact
I will not be eating Marvin

Mushrooms Day 41

Saturday, June 2nd, 2007

Marvin is look very happy today. He’s grown quite a bit overnight again, and is pretty much ready for eating (if you’re into that sort of thing). Tomorrow could well be his last day…

Marvin Day 41 Marving the Happy Mushroom

In the opposite corner of the box, some new fungal growth is occurring, and I think that Marvin may well have some brothers and sisters appearing soon.

A Bientot

Mushrooms Day 40

Friday, June 1st, 2007

The baby ‘shroom isn’t such a baby any more. It’s even starting to resemble a mushroom, which is reassuring. I think I’m going to call it Marvin, partly because I like the alliteration “Marvin the Mushroom”, and partly in recognition of my faithful reader Zaphod.

Marvin

Mushroom Fact
The blue mould in Stilton, the king of cheeses, is Penicillium roqueforti

From: Wikipedia