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.
A PDO database connection requires you to create a new PDO object with a Data Source Name (DSN), Username, and Password. The DSN defines the type of database, the name of the database, and any other information related to the database if required. These are the variables and values we stated inside the dbconfig.
Connecting to a MS SQL Server database with PHP is very similar to connecting to a MySQL database. The following example demonstrates how to connect to a MS SQL database from PHP. Note that the function names contain mssql, not mysql.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With