Quick Tip: Save time on escaping from mysql_real_escape_string()

All data should be escaped before going into a query, to prevent a SQL-injection attack. The current “best practice” is to use mysql_real_escape_string(), which connects to the DB, checks how strings should be escaped, and then returns the safe string.

Unfortunately, this requires a round trip to the DB, and it takes time and resources. Here’s a trick to make things faster.

If the data you’re escaping is supposed to be an integer, and not a string, you can do this:

$_data = (int)$data;

This will force the value to be an integer. There’s no way to do an SQL-injection with a number alone, and this is something that gets done very quickly (as compared to the mysql_real_escape_string).

Central Florida Mountain Biking

Yes, there’s places in Florida worth riding mountain bikes on. It’s not North Carolina or West Virginia (I really want to bike Snowshoe), certainly, but at this time of year, it’ll do.

My buddy Jason is making a map of all the worthwhile biking areas in Central Florida.


View Larger Map

Let me know if there are any places that should get added, and I’ll pass the word along.

Hard work pays off

My local friends know I have been working a lot lately, and not hanging out. They don’t know why, just something about “motorcycles,” “databits,” and “2 AM.”

The Cloudspace team has been working with Lowell Goss of Loud3r, and we’re starting to unveil the fruits of our labor. It’s an amazing system that lets us create sites for people who love to stay informed about a topic. For example, our first site is Fast3r — a motorcycle news site for people who want to keep on top of everything in the motorcycling world. Ducati? Got it. Stunting? Got it.

Somehow, not everyone is interested in motorcycles. So we made another site for people interested in politics, Vot3r. I’ve been our internal beta using it to stay on top of the current political cycle (want to know about Obama?), and it’s amazingly up-to-date. CNN ain’t got nothing on this.

If you’re not interested in bikes or politics, maybe you like video games. For you, there’s Blast3r. I’ve been using it to see what’s coming up next in the gaming world. I’m not a hardcore gamer, but one of the things I like about Blast3r is that it helps me filter though all the BS gaming rumors out there and find the next cool game to play.

Of course if what you really want to keep up on is….well, you get the idea. There’s a lot coming in the next few months.

The Ouroboros, The Recursive Lizard, and Strange Loops

I was reading Reddit the other day and saw a link proclaiming that “the Ouroboros is real!”. Having no idea what this was, I clicked through, and found pictures of a lizard that basically eats it’s own tail when threatened, protecting it’s belly.

Hitting up Wikipedia, I found out that the Ouroboros is a concept that’s been around for years — even Plato is cited as having talked about it. The philisophical idea that it represents seems to be things that come out of themselves; that create and build on themselves; or going backwards, things that “eat” themselves. Things that after they’re done, are the same as when they started. If you were so inclined, you could look at human life like this, where every human essentially has to come from a previous human. This is why the start of human life is such an interesting question.

I find things like this very interesting, especially after reading “An Eternal Golden Braid,” which is a book that is really special in Computer Science. It’s a very large and very intricate book, filled with inside jokes, and another level of inside jokes from there. But it explains some really advanced CompSci concepts through simple stories.

One of the more interesting things in CompSci is recursion, which is a way of solving certain types of problems that involves taking a question, and continually breaking it down into simpler questions, until each simple question has a simple answer, then combining all the simple answers to get an answer to the original question. The trick is that at each level, the simple question is the same type of question as the one that created it, and the same as the question that it will create. For example, if you had 10 people’s names to sort alphabetically, and you split those 10 names into 2 groups of 5, you’d still be alphabetically sorting, but a smaller amount.

This sorting technique is the type of thing that recursion is good at. “Eternal Golden Braid” refers to these type as “strange loops”, because when you break down a question into smaller questions, and end up with the same question, you’ve got something that “eats” itself. Like the Ouroborus — the Recursive Lizard.

How to make a single function call handle one or many

I was browsing through Ning’s codebase the other day. I work directly with the code since I’ve customized a Ning network for one of Cloudspace’s clients in some subtle ways, just because he wanted it that way.

I really like the work they’ve done. Their codebase clearly has a lot of best practices.

I found one neat trick that was used to make a function a bit more versatile, in that it can easily be passed a single object, or an array of objects, with no sweat. If you’re interested, you can find an example of this in /widgets/activity/models/ActivityLogItem.php, in beforeDelete()

This isn’t their code, but here’s an example of the trick.

function eatPie($pie_id) {
    foreach( (is_array($pie_id) ? $pie_id : array($pie_id)) as $current_id) {
        // codez
    }
}

Really straightforward. If an array is passed in, each item in the array gets operated on. If a single item is passed in, it gets turned into an array, then each item in the array gets operated on. I also like how the tertiary statement gets used inline with the foreach. Of course, if you want a bit more code clarity, you can do it like this:

function eatPie($pie_id) {
    $pie_id = is_array($pie_id) ? $pie_id : array($pie_id);
    foreach($pie_id  as $current_id) {
        // codez
    }
}

Drawing diagrams

I sometimes forget just how useful drawing diagrams can be.

For example, working with a DB. Just a quick sketch of circles as tables, write the column titles inside the relevant circles, and then draw a few lines to represent joins.

It’d probably be useful to do this with large blocks of code too.  Just draw parts of the code that matter, and abstract away the parts that don’t matter.