Development refers to software development. I provide development services in a variety of desktop, web, and server languages. Posts in this category are all about interesting development challenges that I’ve run into in my work.

Install the PHP SQLSRV Driver on Linux

Rejoice! Microsoft actually does provide a PHP Linux driver for Microsoft SQL Server. The bad news is, it can be a real pain to get it working.  I went through this experience tonight while setting up a utility I wrote for a client. I ran into two separate issues. This article provides some assistance with the issues I ran into when I tried to Install the PHP SQLSRV driver on Linux.

How to Install PHP SQLSRV Driver on Linux

The instructions in the repository’s readme.md are actually pretty good. Check out the official Github repository for the driver for the latest version and installation information. I recommend you follow their instructions first. When you hit a roadblock, come back to this article for help.

PECL Building with Wrong PHP Version

The final step in the installation is installing the base driver and the PDO variant withpecl install. PECL runs a build process on your machine and it uses a tool called phpize to execute the build. The version of this tool needs to match your PHP version. Otherwise, the build will succeed, but the extension file it generates won’t be compatible with the version of PHP you’re running. You’ll know pretty quickly if this is the case if you get “can’t load extension” errors after you build and execute PHP.

To solve the problem, install the version of phpize that matches your PHP version. For example, if you are running PHP 7.1, you would run:

sudo apt-get install phpize7.1

Microsoft ODBC Client is Missing

After I fixed the problem above, I ran my PHP script and the database connection failed. The PDO SQL Server driver reported an error message that it could not execute because it required the Microsoft ODBC Client. Installing the client is actually part of the steps in the instructions provided on Github. But for me, the installation failed and I didn’t notice. I went back and repeated this step and found that there was a missing dependency that would not install. Once I manually installed it and re-ran the client installation steps, then the PDO driver started working.

The Github repository below contains the official drivers from Microsoft. Check out the repository for the latest version information and installation instructions.

Microsoft Drivers for PHP for SQL Server
https://github.com/microsoft/msphpsql
291 forks.
1,351 stars.
20 open issues.
Recent commits:

When you're totally stuck on a programming problem, sometimes the best course of action is to quit.

Stuck on a Programming Problem? Just Quit.

There comes a time in every developer’s existence when they find themselves stuck on a programming problem.  We’ve all been there:  blood-shot eyes, pulsing veins in our foreheads, and synapses dying at a rate unheard-of since that Robitussin dare you took your senior year of college.

In that moment when Stack Overflow and your own brain have completely let you down, what do you do?

You quit, that’s what.

That’s right: quit. If you’ve already beat your head against a wall for several hours, doing it more eventually stops being productive.

Get up. Step away from the screen. Do something else. Play a video game. Go for a walk. Work on another project. Write something. Build something. Make something. Cook something. Have a conversation. Write some perfectly useless code in a different programming language. 

Whatever you do, do something that shifts your mindset. It might take an hour. It might take a day.  It might take a week. But returning to a project with fresh eyes can make all the difference.

So next time you find yourself staring at the same lines of code a little to long, remember: be a quitter! (Just not, like, permanently. That’s just sad.)

Bulk HTML Validation

I’ve recently been working on a WordPress project in which I made the mistake of starting with a commercial theme that, while beautiful and feature-rich, was a complete mess in terms of code quality. HTML validation is always a requirement on my projects, but a poorly-written WordPress theme combined with WordPress’ own tendency to butcher code by inserting poorly nested <p>’s (more on that later) can make validation a real chore.

The W3C’s Validator is a great tool and the Web Developer Toolbar‘s quick access to it makes the process go even faster.  But with nearly 100 static pages to validate in addition to several hundred imported blog posts written by nontechnical authors, I needed something better.

Enter the Web Design Group’s HTML Validator. This validator isn’t that different from the W3C validator, but it provides an additional key feature: the “validate entire site” option.  By checking this box the validator will spider the URL you provide and validate the first 100 pages that it finds within the same domain.

Subnet Calculator in 9 Lines of PHP

I’ve been spending a lot of time studying for my Microsoft 70-642 exam, an important part of which is subnetting.As an intellectual exercise I wrote a subnet calculator. Enjoy:

1
2
3
4
5
6
7
8
9
 function subnet($hosts)
 {
	$bits       = decbin($hosts) + 2; // Add 2 for Network ID and Broadcast
	$hostBits   = strlen($bits); // find how many bits it takes to represent it
	$cidr       = 32 - $hostBits; // Find slash-notation
	$binaryMask = str_repeat('1', $cidr) . str_repeat('0', $hostBits); // Subnet in binary
	$subnet     = implode('.', array_map('bindec', str_split($binaryMask, 8))); // Subnet in dotted-decimal
	return array('hosts' => $hosts, 'hostBits' => $hostBits, 'cidr' => '/' . $cidr, 'binaryMask' => $binaryMask, 'subnet' => $subnet);
 }

JavaScript Errors in Internet Explorer? It’s Probably a Comma’s Fault

This is a well known bug/feature in Internet Explorer but I’d like to share it again for the sake of beginners, as well as seasoned programmers who forget about it from time-to-time. If you’re testing JavaScript code that seems to work fine in Firefox, Chrome or Safari but chokes in Internet Explorer, a stray comma is probably to blame. Consider the following code:

(Click to Execute)

    arr = [1, 2, 3];
    for(i = 0, c = arr.length; i < c; i++) {
        alert(arr[i]);
    }

This code is dead simple. We create an array with three elements, iterate through that array with a for loop, and display each value. Nothing complicated, and it should run fine in any browser. Now consider a similar block of code:

(Click to Execute)

    arr = [1, 2, 3, ];
    for(i = 0, c = arr.length; i < c; i++) {
        alert(arr[i]);
    }

Notice the different: in the first block of code the array was defined as [1,2,3]. In the second block it is defined as [1,2,3,] with a trailing comma. Firefox, Chrome, and Safari will produce the same output. Internet Explorer, on the other hand, sees a fourth array member which with a value of undefined. Think about it in the context of a complicated JavaScript application. Accidently adding an extra comma to the end of an object or array can produce errors deep within your code or within the libraries they depend on, such as jQuery or ExtJS.

So remember, when troubleshooting JavaScript errors in Internet Explorer, always check for trailing commas, or use a utility like JSLint to find them for you.