Linux环境PHP5.5以上连接SqlServer2008
linux版本:64位CentOS 6.4
Nginx版本:nginx1.8.0
php版本:php5.5.28
Sqlserver版本:2008
FreeTDS版本:0.95
1.首先需要编译安装FreeTDS
说明:一定要从官网下载最新的版本FreeTDS-0.95 ftp://ftp.freetds.org/pub/freetds/stable/freetds-patched.tar.gz
wget ftp://ftp.freetds.org/pub/freetds/stable/freetds-patched.tar.gztar -zxvf freetds-patched.tar.gz cd freetds-0.95
需要注意的就是这里的–with-tdsver=7.3,这个非常重要,你需要根据你的数据库版本选择正确的配置项,由于现在大多是SQLserve2008所以需要选择7.3.
关于这个问题网上有的说是7.1,也有的说是7.2,甚至有的说是8.0,可以看文末参考帖子,不过那些说的都有问题。
造成这个配置项混乱的根源是很多人用的是FreeTDS-0.91,经过我的测试FreeTDS-0.91只支持7.1,如果是7.2以上配置那么通通会变为5.0。
其实参考官网的文档就知道这个问题了,不过由于很多人下载了旧版FreeTDS-0.91,即使设置为–with-tdsver=7.2以上也没有用。
http://www.freetds.org/userguide/ChoosingTdsProtocol.html#idm68282096
总结:FreeTDS-0.91只支持7.1,其余都会默认为5.0。只有最新的FreeTDS-0.95,也就是对Sqlserver2008的最佳配置。
./configure --prefix=/usr/local/freetds --with-tdsver=7.3 --enable-msdblib=make && make install
安装好会看到这样的信息:
2.配置FreeTDS
cd ../echo "/usr/local/freetds/lib/" > /etc/ld.so.conf.d/freetds.conf ldconfig
3.验证FreeTDS版本
这一步非常重要,通过才可以继续,不然后面的步骤都是无意义的。
首先看看版本信息
/usr/local/freetds/bin/tsql -C
测试数据库是否联通
/usr/local/freetds/bin/tsql -H 数据库服务器IP -p 端口号 -U 用户名 -P 密码
很多其他帖子写了需要配置/usr/local/freetds/etc/freetds.conf,其实这个不需要配置。如果配置也可以,配置了PHP就可以调用这个配置项,否则需要PHP代码里指定数据库服务器信息即可。
另外需要注意的是/usr/local/freetds/etc/下的freetds.conf不同于前面/usr/local/freetds/lib/那个freetds.conf。
如果配置了这里,那么PHP页面就可以使用这里的配置,不然PHP页面指定一样可以。
默认是这样的:
$Id: freetds.conf,v 1.12 2007-12-25 06:02:36 jklowden Exp $## This file is installed by FreeTDS if no file by the same # name is found in the installation directory. ## For information about the layout of this file and its settings, # see the freetds.conf manpage "man freetds.conf". # Global settings are overridden by those in a database# server specific section[global] # TDS protocol version; tds version = 4.2 # Whether to write a TDSDUMP file for diagnostic purposes # (setting this to /tmp is insecure on a multi-user system); dump file = /tmp/freetds.log; debug flags = 0xffff # Command and connection timeouts; timeout = 10; connect timeout = 10 # If you get out-of-memory errors, it may mean that your client # is trying to allocate a huge buffer for a TEXT field. # Try setting 'text size' to a more reasonable limit text size = 64512 # A typical Sybase server[egServer50] host = symachine.domain.com port = 5000 tds version = 5.0 # A typical Microsoft server[egServer70] host = ntmachine.domain.com port = 1433 tds version = 7.0
如果你想使用配置项,只要修改[egServer70]即可:
[egServer70] host = 192.168.1.235 这个是数据库服务器IP port = 1433 tds version = 7.1
其他都不用动,关于[egServer70]的名字也是随意的,这个就是给PHP调用的,和PHP代码里一致即可。
4.添加PHP扩展mssql和pdo的pdo_dblib
说明:这2种扩展都可以达到相同的目的,选其一即可。
(1).增加PHP扩展mssql
cd /usr/php-5.5.28/ext/mssql/
linux下用phpize给PHP动态添加扩展
/usr/local/php/bin/phpize./configure --with-php-config=/usr/local/php/bin/php-config --with-mssql=/usr/local/freetds/make && make install
(2).增加PHP扩展pdo的pdo_dblib
cd /usr/php-5.5.28/ext/pdo_dblib/
linux下用phpize给PHP动态添加扩展
/usr/local/php/bin/phpize./configure --with-php-config=/usr/local/php/bin/php-config --with-pdo-dblib=/usr/local/freetds/make && make install
(3).在php.ini配置文件中增加.so
cd /usr/local/php/lib下的php.ini
增加:
extension = "mssql.so"extension ="pdo_dblib.so"
如果你只需要上述2种扩展之一,自然只要新增其中一个的.so扩展到php.ini即可。
(4).重启PHP FastCGI
killall php-fpm/etc/init.d/php-fpm
如果没有正确生成扩展是不能重启php-fpm的。
这时候在phpinfo里就可以看到扩展添加成功的信息了。
5.使用PHP调用SQLserver
(1).mssql_connect配置版
<?phpheader("Content-type: text/html; charset=utf-8");$msdb=mssql_connect("egServer70","blog.csdn.net.unix21","password");if (!$msdb) { echo "connect sqlserver error"; exit; }mssql_select_db("数据库名",$msdb);$result = mssql_query("SELECT top 5 * FROM tablename", $msdb);while($row = mssql_fetch_array($result)) { print_r($row);}mssql_free_result($result);?>
注意:上面的egServer70就是前面freetds/etc/freetds.conf配置的。
(2).mssql_connect非配置版
<?phpheader("Content-type: text/html; charset=utf-8");//$msdb=mssql_connect("数据库IP","blog.csdn.net.unix21","password");//$msdb=mssql_connect("数据库IP:1433","blog.csdn.net.unix21","password");$msdb=mssql_connect("数据库IP:49151","blog.csdn.net.unix21","password");if (!$msdb) { echo "connect sqlserver error"; exit; }mssql_select_db("数据库名",$msdb);$result = mssql_query("SELECT top 5 * FROM tablename", $msdb);while($row = mssql_fetch_array($result)) { print_r($row);}mssql_free_result($result);?>
(3).PDO版本
<?phpheader("Content-type: text/html; charset=utf-8"); try { $hostname = "数据库IP"; $port = 1433; $dbname = "数据库名"; $username = "blog.csdn.net.unix21"; $pw = "password"; $dbh = new PDO ("dblib:host=$hostname:$port;dbname=$dbname","$username","$pw"); } catch (PDOException $e) { echo "Failed to get DB handle: " . $e->getMessage() . "\n"; exit; } $stmt = $dbh->prepare("SELECT top 5 * FROM tablename"); $stmt->execute(); while ($row = $stmt->fetch()) { print_r($row); } unset($dbh); unset($stmt); ?>
显示数据:
4.sqlsrv版
$conf = array( 'info' => array("UID" => 'ui', "PWD" => 'scard', "Database" => 'post'), 'host' => '500.100.139.250', //248 ); $connectionInfo = $conf['info']; $host = $conf['host']; $conn = sqlsrv_connect($host, $connectionInfo); if ($conn == false) { echo "连接数据库失败!"; echo "<pre>"; print_r(sqlsrv_errors()); die; } $sql = "SELECT * FROM BS_ROLE_INDEX_INFO"; $query = sqlsrv_query($conn, $sql, array(
推荐本站淘宝优惠价购买喜欢的宝贝:
本文链接:https://www.hqyman.cn/post/11943.html 非本站原创文章欢迎转载,原创文章需保留本站地址!


休息一下~~