linux centos6.4 php连接sql server2008
1、安装SQL Server驱动freetds
yum search freetds
yum install freetds php-mssql
或者下载编译安装
2、修改/etc/freetds.conf
[server2008]
host = 192.168.70.119
port = 1433
tds version = 7.0
client charset = utf8
最好重启一下apache
3、测试连接
./tsql -S 192.168.0.109 -U 用户名 -P 密码 -D 数据库
如果可以有看到1>说明连接成功
输入sql语句
1>select * from table;
2>go
4、代码测试连接
<?php $server = 'server2008 '; $link = mssql_connect($server, 'sa', '123456'); if (!$link) { die('Something went wrong while connecting to MSSQL'); } ?>
5、测试PDO连接Sql server
这里用到了dblib
<?php $db = new PDO("dblib:host=192.168.70.119;dbname=master","sa","123456"); $sql = "select top 3 * from spt_values"; $res = $db->query($sql); while ($row = $res->fetch()){ print_r($row); } $res = null; $db = null; ?>
结果:
Array ( [name] => jiqing [0] => jiqing [number] => 1 [1] => 1 [type] => A [2] => A [low] => [3] => [high] => [4] => [status] => 0 [5] => 0 ) Array ( [name] => pub [0] => pub [number] => 2 [1] => 2 [type] => A [2] => A [low] => [3] => [high] => [4] => [status] => 0 [5] => 0 ) Array ( [name] => sub [0] => sub [number] => 4 [1] => 4 [type] => A [2] => A [low] => [3] => [high] => [4] => [status] => 0 [5] => 0 )