Lost connection to MySQL server during query rows

While working on a project today a script I had that turns a database into a simple tab delimited file stopped working and was producing the following error:

Lost connection to MySQL server during query rows

Google searching didn’t turn up anything useful because most people said just to “use mysqldump instead” which wasn’t really an option for me. After running analyze, repair and optimize table and using phpMyAdmin to make a copy of the table I was pretty confident that it wasn’t a corruption problem so I added the following bit of code around what I already had:

 

define(‘C_NUM_PER_LIMIT’, 100000);

$iteration = 0;

do
{
$limit_from = C_NUM_PER_LIMIT * $iteration; $iteration++;

$sql = “SELECT * FROM `table` ORDER BY `id` LIMIT $limit_from, ” . C_NUM_PER_LIMIT;
$res = mysql_query($sql);
// way more stuff

} while (mysql_num_rows($res) > 0);

I hope this helps. :)

Working with CSV files in PHP

This is the best and simplest way to work with a CSV file that has the headers on the first row in PHP.


if (($handle = fopen('csv.csv', 'r')) !== false)
{
$length = 0;
$delimiter = ',';
$enclosure = '"';
$escape = '\\';

if(($header_line = fgetcsv($handle, $length, $delimiter, $enclosure, $escape)) !== false)
{
while(($line = fgetcsv($handle, $length, $delimiter, $enclosure, $escape)) !== false)
{
$data = array_combine($header_line, $line);
//do stuff here
}
}

fclose($handle);
}

Jack Layton


I know it’s a little late but I wanted to make a quick post to commemorate the passing of one of Canada’s greatest politicians back on August 22nd, 2011. That politician was, of course, Mr. Jack Layton.

Hearing Jack speak was so refreshing. His optimism gave hope to all Canadians. His energy, passion and drive are things that we should all aspire to emulate. His vision – a country of greater equality, justice, and opportunity – is one that benefits all Canadians. I salute you Mr. Layton for challenging all of us to be better.

As I was looking for photos to insert into this post I also came across this:

Jack was a Trekkie! :D

Got a packet bigger than ‘max_allowed_packet’ bytes

Ran in to this problem today at work so I’m just making a note of it for future reference. I was running a PHP script crawler today that was inserting the downloaded data into a MySQL database and the script crash with an error:

Got a packet bigger than ‘max_allowed_packet’ bytes

The simple fix was to add in the code:

$sql = "SET GLOBAL max_allowed_packet=16*1024*1024;"; // 16 * 1 MB
mysql_query($sql);

Hope this helps someone else as well. :)

Train from Belfast to Dublin

During the planing of our vacation this year I had to book a train from Belfast to Dublin. Most places including the Lonely Planet Ireland book tell you that you can book a train on the Iarnród Éireann website. As soon as you try to book a ticket from Belfast Central to Dublin Connolly though you get the message:

You cannot make a booking that originates in Northern Ireland. Please contact Translink on 048 9089 9409 Monday to Friday 09:00-17:00 to book a ticket

Now if you’re doing this from another country and don’t want to make a long distance call you’re stuck. Luckily, Translink has a website. An “Enterprise” route will get you to Dublin Connolly at a reduced webfare. After purchasing the confirmation email I received said something about them mailing the ticket. A quick followup email got them to ensure the tickets were waiting for us at the station.

Hopefully this helps someone. :)

Vacation 2011 – The British Isles

Kari and I with Bill from Real Scottish Journeys

Kari and I with Bill from Real Scottish Journeys

So Kari and I decided to go to The British Isles this year for our summer vacation. Specifically, we went to London – England, Salisbury – England, Edinburgh – Scotland, Portree – Scotland, Belfast – Ireland and Dublin Ireland. Personally, I enjoyed Scotland the most thanks to the man pictured above – Bill that runs an amazing tour company in Scotland named Real Scottish Journeys. Bill took us on a wonderful tour to the Isle of Skye during which we saw many incredible things and listened to many fascinating stories.

The other awesome thing about our trip was the quality of beer. It’s actually been pretty hard drinking the beer here in Canada as the beer we found on our trip was about 50 times better than the stuff we drink in Canada. The Guinness we drank in Ireland was so smooth and delicious compared to the slightly bitter and slightly less smooth version found here. I also found a new appreciation for Smithwick’s and was utterly disappointed when I ordered one here in Canada recently and discovered that it tasted nothing like those I had in Ireland.

Finally, another warm thanks goes out to Rory Laden who was our awesomely gracious host in Dublin, Ireland. He was really helpful with respect to suggesting things to do while we were in Dublin and gave us free reign over his kitchen in order to make breakfast/lunch.

I can’t wait until our next vacation. :)