您的位置:首页 > 运维架构 > Linux

Linux环境PHP5.5以上连接SqlServer2008【全网最经典无错版】

2015-08-12 21:06 567 查看
linux版本:64位CentOS 6.4Nginx版本:nginx1.8.0php版本:php5.5.28Sqlserver版本:2008FreeTDS版本:0.95
关于Linux环境安装Nginx+PHP参考《Linux环境Nginx安装与调试以及PHP安装 》即可。 一般来说,PHP+mysql是最经典的组合,跑在Linux环境是非常好的,如果是PHP+Sqlserver是跑在windows环境下的。今天需要Linux环境下PHP调用Sqlserver,用了一天的时间,终于把这个问题彻底研究清楚,网上其他类似文章我大都看了,其实有的是因为太久远不适用,有的有错误,还有的有几个关键问题没有说清楚,看此文其他可以忽略了,说真的踩坑真的很累,也没必要,照着这篇来做就是,所以本文才号称是全网最经典无错版,其实这么说主要是希望大家节省时间。1.首先需要编译安装FreeTDS说明:一定要从官网下载最新的版本FreeTDS-0.95 ftp://ftp.freetds.org/pub/freetds/stable/freetds-patched.tar.gz如果官网实在太慢建议从本人上传的这里一样很快下载:http://download.csdn.net/detail/21aspnet/9000357
# wget ftp://ftp.freetds.org/pub/freetds/stable/freetds-patched.tar.gz
# tar -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以上也没有用。


总结: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 安装好会看到这样的信息:


配置FreeTDS# cd ../# echo "/usr/local/freetds/lib/" > /etc/ld.so.conf.d/freetds.conf
# ldconfig 验证FreeTDS版本这一步非常重要,通过才可以继续,不然后面的步骤都是无意义的。首先看看版本信息# /usr/local/freetds/bin/tsql -C


测试数据库是否联通# /usr/local/freetds/bin/tsql -H 数据库服务器IP -p 端口号 -U 用户名 -P 密码

关于freetds/etc/freetds.conf配置项很多其他帖子写了需要配置/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代码里一致即可。 3.添加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里就可以看到扩展添加成功的信息了。



4.使用PHP调用SQLserver(1).mssql_connect配置版
<?php
header("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非配置版
<?php
header("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版本
<?php
header("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);

?>
显示数据:


以上本人都是验证过的。 参考信息:1.这个帖子解释了第一个重要问题为什么是--with-tdsver=7.1http://blog.csdn.net/dlutxie/article/details/68514292.官方信息http://php.net/manual/zh/ref.pdo-dblib.phphttp://php.net/manual/zh/function.mssql-connect.php3.本人之前也是参考这些的,但是有些地方有错误,网上主要也就这些帖子,其他都是互相引用的。http://zyan.cc/php_sqlserver_freetds/http://my.oschina.net/robanlee/blog/168626?p={{currentPage+1}}http://blog.csdn.net/rgb_rgb/article/details/8762769http://blog.sina.com.cn/s/blog_87b9bbc70101avmh.htmlhttp://stackoverflow.com/questions/13180746/mssql-connect-unable-to-connect-to-server-without-freetds-confhttp://blog.csdn.net/chenjiebin/article/details/7278304
http://blog.163.com/koumm@126/blog/static/9540383720112157055119/
http://www.linuxidc.com/Linux/2012-09/70192.htm
http://blog.csdn.net/helonsy/article/details/7207497/article/3497948.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: