Skip to main content

Posts

Sending Mail in PHP

Email sucks, and it's incredibly broken! I mean.... sending an email in Gmail is obviously quite trivial.  But working with SMTP and email servers... not so much. Most people take email for granted.  It was invented way before I was born... way before Windows 95, Windows 3.1, or even DOS!  What?!?! Yeah... so not surprisingly, email sucks.  Remember... DOS sucked and Windows 95 sucked (not at that time, of course).  In fact, email sucks so much, it makes me sad that we still use it.  It's almost embarrassing... almost as embarrassing as Internet Explorer. Don't believe me?  Just go to the Postfix mail server documentation page.  Their website is straight out of the late-80's.  I think there might be one  image on the entire website?  After all, we can't have those 4.8K modems downloading images... it will take too long, tie up your phone line, and waste too much bandwidth.  :) So, why am I complaining about email? ...

Minesweeper Fail!

I'm pretty sure that if I've played 100 games and won exactly 1 of them, my winning percentage should be 1%. I might be wrong, though.  There is no rounding error when you do 1/100 * 100%.  Single-precision floating point number rounding error?  For some reason, I doubt it... Weird... and stupid... EDIT:  Wrote a C program to make sure that Microsoft's Minesweeper has a bug. $ cat main.c #include <stdio.h> int main() {         float x = 1.0 / 100 * 100;         printf("%f\n", x);         return 0; } $ gcc main.c; ./a.out 1.000000 ... Yep... sure does.

Improving query performance - Subqueries (*aka Derived Queries)

I recently asked  this question on StackOverflow  about how to join to a derived table using an index.  I found out that even if your derived table is generated with tables that use all of the proper indexes, and you try to join that derived table to a base table in your outer query, no indexes are used.  Ever... So, let's start with a contrived example (as we often do)... SELECT * FROM t1 JOIN (SELECT * FROM t2) AS derived_t2 ON t1.f1=derived_t2.f1; OK... so let me get this straight... even if t1.f1 and  t2.f1  are indexed, the join will still be a full table scan?  You betcha! Let me say that again...  whenever you write a subquery and try to join the derived table to your outer query tables, no indexes are ever used .  It's a full table scan. So, I think to myself... "Wow... so that's really stupid, actually... because I have tables that only have a few thousand rows in them, and the query performance is shit.  Like... ...

Node.JS + MySQL + Transactions

If you're like me, then you are probably building web applications using Node.JS and MySQL (and maybe Redis, too).  If so, you're probably going to need transactions, and you've probably already noticed that the current version of node-mysql doesn't support transactions yet.  :( But that's OK because I have a solution for you.  Check out node-mysql-queues on github .  This project provides pretty good support for MySQL transactions with a fairly simple API.  There are a couple of things to remember, though.  For one, Node.JS is very "callback-centric," so when executing a series of queries, you would normally chain the queries together with a series of callbacks.  node-mysql sort of changes this model, by allowing you to place queries on a queue to be executed in order.  If you only care about doing something when all of your queries are done, you can simply put your callback in the final query.  node-mysql-queues allows you to do the same...

Query Pivot in MySQL

Basically this blog post is about dynamic columns in MySQL queries.  Since you can't run "pivot" queries in MySQL, how do you write queries that return an arbitrary number of columns (perhaps based on the number of rows in another table)? The answer is simple: you can't... at least not in a single query.  The best way to do this is to run two queries and generate the SQL for the 2nd query based on the results of the 1st query. Imagine this table of product sales: ProductID CustomerID Unit Sales 1 1 20 1 2 50 2 1 20 2 2 70 2 3 90 Maybe you want your result set to look like this... ProductID Customer 1 Customer 2 Customer 3 1 20 50 0 2 20 70 90 Write this query first: SELECT CustomerID, CustomerName FROM `customers` Store the results into $rows.  For each $row, append this string to $fields: ", SUM(CASE WHEN CustomerID=$row[0] THEN `Unit Sales` ELSE 0 END) AS $row[1]" Then... write this query: "SELECT ProductID ...

IE Sucks

I am so frustrated right now that I am going to dedicate an entire blog post to how crappy Internet Explorer is. With all due respect, Microsoft, IE is terrible. Thank you for your understanding. Sincerely, The Management

Why Node.JS?

This article is for those of us who have heard all of the buzz about Node.JS and they need to convince their boss or their clients to make the switch.  Or, perhaps... you're not quite convinced yet? Why Node.JS? Reason #1 - JavaScript is awesome.  So, in case you didn't know, JavaScript is awesome.  I'll let you read my other blog post  for more details, but I'll also summarize for you: The language itself is extremely powerful.  JavaScript comes packed with modern programming language features like closures, dynamic data types, first-class and higher-order functions, prototypes (not classes), lack of concurrency  (more on this later), exception handling, garbage collection, and much, much more!  These features help make your code better, keep you from making mistakes, and make your applications more bulletproof. JavaScript is widely adopted.  It's the scripting language of the web.  Need I say more?  Almost any device with a web...