Eric Z Beard
Eric Z Beard

Reputation: 38406

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

If you search Google for this question, you will find a lot of incorrect, misleading, and outdated information. Surprisingly, there isn't a solid answer on Stack Overflow, so we should change that.

I am using the Mac port installation of Apache and PHP. I have installed php5-mssql, and I can see mssql on my phpinfo() page.

But I don't see it listed under PDO.

PDO support enabled
PDO drivers     dblib, mysql, odbc, pgsql 

Is mssql not associated with PDO? Is there another driver that can be used on a Mac to connect to a SqlServer database using PDO? Seems like this is something that should be possible.

Upvotes: 29

Views: 25459

Answers (5)

E_p
E_p

Reputation: 3144

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);

Upvotes: 5

xgretsch
xgretsch

Reputation: 1423

Note that Microsoft have published a PHP7 extension for this, but if you're still on PHP5.x, that doesn't help you. I've succeeded in connecting using a different stack: freetds,odbc,pdo.

I'm using OS X 10.11.6 (El Capitan) with Macports, PHP5.6.

I've started by creating an Azure SQL Database called mydb on a server with a name of myserver.database.windows.net. It's important to remember to open the firewall to your client IP address, which you do on the server.

First step is to install freetds with the ODBC driver, and its PHP connector (change php56 to the correct version of your PHP):

sudo port install freetds +odbc
sudo port install php56-odbc

Next, you need to include some lines in your configuration files:

/opt/local/etc/odbcinst.ini

[FreeTDS]
    Description = ODBC for FreeTDS
    Driver      = /opt/local/lib/libtdsodbc.so
    Setup       = /opt/local/lib/libtdsodbc.so
    FileUsage   = 1

This tells the odbc library where to find its odbc driver.

/opt/local/etc/freetds/freetds.conf

[myserver]
    host = myserver.database.windows.net
    port = 1433
    tds version = 7.0

This tells the freetdc library where to find your server.

/opt/local/etc/odbc.ini

[myds]
Description = Test for SQL Server on Azure
Driver = FreeTDS
Trace = Yes
TraceFile = /var/log/sql.log
Database = mydb
Servername = myserver
UserName = myusername
Password = mypassword
Port = 1433
Protocol = 7.0
ReadOnly = No
RowVersioning = No
ShowSystemTables = No
ShowOidColumn = No
FakeOidIndex = No

This creates a data source called myds pointing at your database, enabling you to connect with the following PHP:

$conn = new PDO('odbc:myds', 'myusername', 'mypassword');

If any of this doesn't work for you, first check that the freetds installation is correct using:

tsql -S myserver -U myusername -P mypassword

And then check that the ODBC specifications are OK using:

isql -v myds myusername mypassword

Thanks to https://github.com/lionheart/django-pyodbc/wiki/Mac-setup-to-connect-to-a-MS-SQL-Server, which does the equivalent job for Python and which pointed me in the right direction for all this.

Upvotes: 1

holm50
holm50

Reputation: 892

Thanks Esteban for the nice guide (https://stackoverflow.com/a/37707426) which succesfully helped me install the pdo_dblib driver.

However, I was having issues connecting to my Azure SQL database from OSX 10 with PHP (5.5) and FreeTDS using the provided dblib dsn. What finally fixed it for me was appending the Azure database (m53man42a) to my username.

My dblib PDO connection in PHP:

$conn = new PDO("dblib:host=azure-sql;dbname=my-database-name", 
"username@m53man42a",
"my-secret-password");

My FreeTDS.conf:

[azure-sql]
host = m53man42a.database.windows.net
port = 1433
tds version = 8.0
client charset = UTF-8
text size = 20971520

Consider adding this as a bullet number 10 in your list... :D

Upvotes: 1

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.

Steps 1.- 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.ini extension=pdo_dblib.so

8.- Restart Apache

9.- Connect using the dblib dsn:

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

Upvotes: 5

Benny Hill
Benny Hill

Reputation: 6240

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

Upvotes: 16

Related Questions