Você está na página 1de 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

Você também pode gostar