安装 FreeTDS 和 PHP MSSQL 扩展

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

重新启动 Apache

sudo /etc/init.d/apache2 restart

测试 FreeTDS

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

配置 FreeTDS

sudo nano /etc/freetds/freetds.conf

在最后一行添加:

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

通过已配置文件服务器名测试 FreeTDS

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

在 PHP 代码中测试

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