How to: Using MS SQL (dynamic ports) in PHP 7 with ODBC PDO

So I’ve just spent a painful number of hours to figure out how to connect to a Microsoft SQL Database (MS SQL) that’s using dynamic ports and named instances (if you’ve got a feeling I’m definitely not a M$ guy … you would be right).

Because I didn’t find a proper working how to on doing this and had to resort to reading mailinglist archives and far, far too many stackoverflow threads I’m posting everything here before I do my best to forget it.

Note: I’m using Arch (Manjaro) but it should work the same way on Ubuntu – just update paths and install commands.

Step 1: Install required packages

sudo pacman -S odbc php-odbc freetds

Step 2: Enable extensions in php.ini

Uncomment extension=pdo_odbc.so

Make sure to restart PHP-FPM after you’re done.

Step 3: Configure ODBC and FreeTDS

Edit odbcinst.ini

sudo nano /etc/odbcinst.ini
[FreeTDS] # This is your identifier! You'll need it in odbc.ini
Description = FreeTDS Driver v0.91
Driver = /usr/lib/libtdsodbc.so # THIS IS ARCH SPECIFIC PATH
fileusage=1
dontdlclose=1
UsageCount=1

If you’re not on arch you’ll have to find the location of libtdsodbc.so for your distro, or you can just use locate to find it.

Edit odbc.ini

sudo nano /etc/odbc.ini
[mssqlconn] # This is important - you'll be refering to your DB with this
Description = MSSQL Server
Driver = FreeTDS # Same as identifier in odbcinst.ini
Database = NAME_OF_DATABASE # Not instance, database within the instance
ServerName = mssqlconn # This  is the same identifier as in freetds.conf
TDS_Version = 8.0

 

Edit freetds.conf

sudo nano /etc/freetds/freetds.conf

Add this to the end:

[mssqlconn]
host = # HOSTNAME or IP of your server
instance = # Name of the INSTANCE (not DB)
tds version = 8.0

Step 4: Test connection

osql -S mssqlconn -U 'username' -P 'password'

If this works, you’re good to go!

Step 5: Connect from PHP

$serverName = 'mssqlconn';
$dbName = 'YourDb';

$pdo = new PDO(sprintf("odbc:DRIVER=FreeTDS;SERVERNAME=%s;DATABASE=%s", $serverName, $dbName), $username, $pass);

 

That’s it! The rest is standard PDO stuff.

 

Kako naložiti AdBlock?

Adblock je program za blokiranje oglasov na spletu. Program, ki ga naložite v vaš brskalnik (“browser”) deluje tako da prepoznava in blokira oglase še preden se naložijo na vaš računalnik.

Tako vam prihrani ne le mentalni napor ignoriranja oglasov, ampak tudi pospeši nalaganje strani, zmanjša obremenitev računalnika in (!) pomaga varovati vašo zasebnost.

AdBlock programov je več. Včasih popularnega Adblock Plus* je sedaj nadomestil hitrejši uBlock origin. Kako jih naložiti je pa odvisno od tipa vašega brskalnika.
Continue reading

Facebook chat is locking you in.

Like many I use Facebook Chat not because I like it but because I have no other choice. I know, “you can use X, it’s way better”. But all of the alternatives have one fatal flaw: People don’t use them*.

And this is exactly what Facebook wants.

So, now they have the user base … and they are determined not to lose it.
Continue reading

Effective Transparency: What transparency isn’t, but should be

The veil of transparency has long been used and abused to legitimize otherwise undemocratic decisions. When confronted with criticism that some legislative process was exclusionary, it is often defended with “That’s not correct, all information was posted on the public bulletin board.” Yes it may as well have been, but the fact that that board is located in an unused hallway at the back of the Town Hall, where no one ever visits, does not mitigate the fact that the process was exclusionary. Continue reading

Vaccinations: Don’t blame the parents

I’ve been following the anti-vaxxer movement for a while now – my elderly father is a proud member – but the latest news headlines have prompted me to speak out.
This post is not intended to persuade you to vaccinate your kids, but to convince the “skeptics” community to stop making things worse.

Parents are not idiots for being taken advantage of.

The anti-vaxxer “movement” is fraud, plain and simple. Continue reading