0% found this document useful (0 votes)
71 views2 pages

Connecting PHP To Microsoft SQL Server On Linux - Blog - Dave James Miller

This document provides steps to connect PHP on Linux to a Microsoft SQL Server database. It involves installing FreeTDS and the PHP MS SQL extension, configuring FreeTDS, and testing the connection from PHP. The key steps are: 1) Install required packages, 2) Configure FreeTDS to specify the server, port, and TDS version, 3) Test the connection from the command line and PHP.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views2 pages

Connecting PHP To Microsoft SQL Server On Linux - Blog - Dave James Miller

This document provides steps to connect PHP on Linux to a Microsoft SQL Server database. It involves installing FreeTDS and the PHP MS SQL extension, configuring FreeTDS, and testing the connection from PHP. The key steps are: 1) Install required packages, 2) Configure FreeTDS to specify the server, port, and TDS version, 3) Test the connection from the command line and PHP.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

7/23/2016

Connecting PHP to Microsoft SQL Server on Linux - Blog - Dave James Miller

Connecting PHP to Microso t SQL Server on Linux


23 April 2011

Here is how to get PHP 5.2 on Linux (speci cally Debian/Ubuntu) talking to a Microsoft SQL Server
database:

1. Install FreeTDS and the PHP MS SQL extension

sudo apt-get install freetds-common freetds-bin unixodbc php5-sybase

Note: That is correct, the MS SQL extension is in the php5-sybase package.

2. Restart Apache

sudo /etc/init.d/apache2 restart

3. Test FreeTDS

tsql -H your.server.name -p 1433 -U yourusername -P yourpassword -D yourdatabasename

If it connects, its working. Note: If you try to SELECT an NTEXT or NVARCHAR column you may get
an error saying Unicode data in a Unicode-only collation or ntext data cannot be sent to clients
using DB-Library (such as ISQL) or ODBC version 3.7 or earlier. That is expected and will be xed
in the next step.

4. Con gure FreeTDS

sudo vim /etc/freetds/freetds.conf

Add this at the end of the le:

1
2
3
4

[yourserver]
host = your.server.name
port = 1433
tds version = 8.0

5. Test FreeTDS using server name


https://davejamesmiller.com/blog/connecting-php-to-microsoft-sql-server-on-linux

1/2

Connecting PHP to Microsoft SQL Server on Linux - Blog - Dave James Miller
5. Test FreeTDS using server
name

7/23/2016

tsql -S yourserver -U yourusername -P yourpassword -D yourdatabasename

If you try to select something, you shouldnt get the Unicode error now because you speci ed
tds version = 8.0.

6. Test in PHP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

$link = mssql_connect('yourserver', 'yourusername', 'yourpassword');

if (!$link)
die('Unable to connect!');

if (!mssql_select_db('yourdatabasename', $link))
die('Unable to select database!');

$result = mssql_query('SELECT * FROM yourtable');

while ($row = mssql_fetch_array($result)) {


var_dump($row);
}

mssql_free_result($result);

https://davejamesmiller.com/blog/connecting-php-to-microsoft-sql-server-on-linux

2/2

You might also like