How can I connect to Sql Server from a Mac with PHP PDO? How can I connect to Sql Server from a Mac with PHP PDO? sql-server sql-server

How can I connect to Sql Server from a Mac with PHP PDO?


Does this help you?

http://blog.nguyenvq.com/2010/05/16/freetds-unixodbc-rodbc-r/

I use FreeTDS to connect to Microsoft SQL servers from a Linux server and it looks like the person in the link above has used FreeTDS to connect from a Mac.

Here is my /etc/freetds/freetds.conf file (the only part I added was at the very end for the XYZ server):

[global]        # TDS protocol version;       tds version = 4.2        # Whether to write a TDSDUMP file for diagnostic purposes        # (setting this to /tmp is insecure on a multi-user system);       dump file = /tmp/freetds.log;       debug flags = 0xffff        # Command and connection timeouts;       timeout = 10;       connect timeout = 10        # If you get out-of-memory errors, it may mean that your client        # is trying to allocate a huge buffer for a TEXT field.          # Try setting 'text size' to a more reasonable limit         text size = 64512# Define a connection to the MSSQL server.[xyz]        host = xyz        port = 1433        tds version = 8.0

[Edit by the asker]

FreeTDS configuration is the first half of the answer. Once it's configured you should be able to run something like this from the command line and connect:

tsql -S xyz -U username -P password

Then you need to use dblib, not mssql, as the PDO driver:

$pdo = new PDO("dblib:host=$dbhost;dbname=$dbname",                "$dbuser","$dbpwd");

Where $dbhost is the name from the freetds.conf file


After looking at many threads, I've found that the best way to connect to MSSQL from Mac OS X with PHP 7 or older is to use dblib. (Just download the correct php version)

You can follow these instructions (ignoring the mssql.so extension) to connect very easily:

https://github.com/BellevueCollege/public-docs/blob/master/PHP/configure-mssql-pdodblib-mac.md

It worked perfect with OS X El Capitan, Bitnami with PHP 7.

Steps1.- Install XCode

$ xcode-select ---install

2.- Install Homebrew

3.- Install autoconf using Homebrew.

$ brew install autoconf

4.- Install FreeTDS

$ brew install freetds

5.- Download your version of PHP Source and uncompress it.

6.- Build the PDO DBLIB extension (Example for PHP 5.5.14)

$ cd php-5.5.14/ext/pdo_dblib$ phpize$ ./configure --with-php-config=/usr/bin/php-config --with-pdo-dblib=/usr/local/$ make$ sudo cp modules/pdo_dblib.so /usr/lib/php/extensions/no-debug-non-zts-20121212

7.- Add the .so extensio to php.iniextension=pdo_dblib.so

8.- Restart Apache

9.- Connect using the dblib dsn:

$pdo = new PDO("dblib:host=$dbhost;dbname=$dbname","$dbuser","$dbpwd");


dblib is the driver that need to be used with mssql on unix systems

No need for you to install anything else,

<?php    $dsn = 'dblib:dbname=testdb;host=127.0.0.1';    $user = 'dbuser';    $password = 'dbpass';    $dbh = new PDO($dsn, $user, $password);