初试PHP连接sql server
最开始想使用 pdo_sqlsrv 拓展,但是一直没成功,本文采用的是 pdo_dblib + freetds。
环境:CentOS 6.8、PHP 5.6.20
freetds
1 wget ftp://ftp.freetds.org/pub/freetds/stable/freetds-1.00.109.tar.gz 2 tar -xzf freetds-1.00.109.tar.gz 3 cd freetds-1.00.109/ 4 ./configure --prefix=/datas/soft/freetds --enable-msdblib --with-tdsver=7.1 5 make 6 make install 7 /datas/soft/freetds/bin/tsql -C
pdo_dblib
1 cd ~/php-5.6.20/ext/pdo_dblib 2 /datas/soft/php56/bin/phpize 3 ./configure --with-php-config=/datas/soft/php56/bin/php-config --with-pdo-dblib=/datas/soft/freetds 4 make 5 make install 6 vim /datas/soft/php56/etc/php.ini 7 extension = "pdo_dblib.so" 8 php --ri pdo_dblib
测试代码
1 $db = new PDO ("dblib:host=IP:端口;dbname=数据库名","用户名","密码"); 2 $stmt = $db->prepare("SELECT top 5 * FROM my_table"); 3 $stmt->execute(); 4 while ($row = $stmt->fetch()) { 5 var_dump($row); 6 }
另外,我这边用的是 php-5.6.20 安装包里自带的拓展包去编译 pdo_dblib,偶然有一次是从 http://pecl.php.net/package/PDO_DBLIB 下载的,会出现以下错误
1 checking for re2c... no 2 configure: WARNING: You will need re2c 0.13.4 or later if you want to regenerate PHP parsers. 3 checking for gawk... gawk 4 checking for PDO_DBLIB support via FreeTDS... yes, shared 5 configure: error: Directory /datas/soft/freetds is not a FreeTDS installation directory
参考资料
https://blog.csdn.net/lanwilliam/article/details/79557119
https://blog.csdn.net/qq_20259383/article/details/77838209
https://www.aliyun.com/jiaocheng/126744.html
http://www.freetds.org
https://blog.slogra.com/post-421.html
https://sourceforge.net/projects/re2c/files/
https://www.linuxidc.com/Linux/2008-09/15686.htm