1. 程式人生 > >Linux環境PHP5.5以上連接SqlServer2008

Linux環境PHP5.5以上連接SqlServer2008

文檔 rec rep content linux下 lee log 時間 以及

linux版本:64位CentOS 6.4

Nginx版本:nginx1.8.0

php版本:php5.5.28

Sqlserver版本:2008

FreeTDS版本: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頁面指定一樣可以。

默認是這樣的:

[cpp] view plain copy
  1. # $Id: freetds.conf,v 1.12 2007-12-25 06:02:36 jklowden Exp $
  2. #
  3. # This file is installed by FreeTDS if no file by the same
  4. # name is found in the installation directory.
  5. #
  6. # For information about the layout of this file and its settings,
  7. # see the freetds.conf manpage "man freetds.conf".
  8. # Global settings are overridden by those in a database
  9. # server specific section
  10. [global]
  11. # TDS protocol version
  12. ; tds version = 4.2
  13. # Whether to write a TDSDUMP file for diagnostic purposes
  14. # (setting this to /tmp is insecure on a multi-user system)
  15. ; dump file = /tmp/freetds.log
  16. ; debug flags = 0xffff
  17. # Command and connection timeouts
  18. ; timeout = 10
  19. ; connect timeout = 10
  20. # If you get out-of-memory errors, it may mean that your client
  21. # is trying to allocate a huge buffer for a TEXT field.
  22. # Try setting ‘text size‘ to a more reasonable limit
  23. text size = 64512
  24. # A typical Sybase server
  25. [egServer50]
  26. host = symachine.domain.com
  27. port = 5000
  28. tds version = 5.0
  29. # A typical Microsoft server
  30. [egServer70]
  31. host = ntmachine.domain.com
  32. port = 1433
  33. tds version = 7.0

如果你想使用配置項,只要修改[egServer70]即可:

[cpp] view plain copy
  1. [egServer70]
  2. host = 192.168.1.235 這個是數據庫服務器IP
  3. port = 1433
  4. 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

增加:

[cpp] view plain copy
  1. extension = "mssql.so"
  2. 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] view plain copy
  1. <?php
  2. header("Content-type: text/html; charset=utf-8");
  3. $msdb=mssql_connect("egServer70","blog.csdn.net.unix21","password");
  4. if (!$msdb) {
  5. echo "connect sqlserver error";
  6. exit;
  7. }
  8. mssql_select_db("數據庫名",$msdb);
  9. $result = mssql_query("SELECT top 5 * FROM tablename", $msdb);
  10. while($row = mssql_fetch_array($result)) {
  11. print_r($row);
  12. }
  13. mssql_free_result($result);
  14. ?>

註意:上面的egServer70就是前面freetds/etc/freetds.conf配置的。

(2).mssql_connect非配置版

[php] view plain copy
  1. <?php
  2. header("Content-type: text/html; charset=utf-8");
  3. //$msdb=mssql_connect("數據庫IP","blog.csdn.net.unix21","password");
  4. //$msdb=mssql_connect("數據庫IP:1433","blog.csdn.net.unix21","password");
  5. $msdb=mssql_connect("數據庫IP:49151","blog.csdn.net.unix21","password");
  6. if (!$msdb) {
  7. echo "connect sqlserver error";
  8. exit;
  9. }
  10. mssql_select_db("數據庫名",$msdb);
  11. $result = mssql_query("SELECT top 5 * FROM tablename", $msdb);
  12. while($row = mssql_fetch_array($result)) {
  13. print_r($row);
  14. }
  15. mssql_free_result($result);
  16. ?>


(3).PDO版本

[php] view plain copy
  1. <?php
  2. header("Content-type: text/html; charset=utf-8");
  3. try {
  4. $hostname = "數據庫IP";
  5. $port = 1433;
  6. $dbname = "數據庫名";
  7. $username = "blog.csdn.net.unix21";
  8. $pw = "password";
  9. $dbh = new PDO ("dblib:host=$hostname:$port;dbname=$dbname","$username","$pw");
  10. } catch (PDOException $e) {
  11. echo "Failed to get DB handle: " . $e->getMessage() . "\n";
  12. exit;
  13. }
  14. $stmt = $dbh->prepare("SELECT top 5 * FROM tablename");
  15. $stmt->execute();
  16. while ($row = $stmt->fetch()) {
  17. print_r($row);
  18. }
  19. unset($dbh); unset($stmt);
  20. ?>

顯示數據:

以上本人都是驗證過的。

參考信息:

1.這個帖子解釋了第一個重要問題為什麽是--with-tdsver=7.1

http://blog.csdn.net/dlutxie/article/details/6851429

2.官方信息

http://php.net/manual/zh/ref.pdo-dblib.php

http://php.net/manual/zh/function.mssql-connect.php

3.本人之前也是參考這些的,但是有些地方有錯誤,網上主要也就這些帖子,其他都是互相引用的。

http://zyan.cc/php_sqlserver_freetds/

http://my.oschina.net/robanlee/blog/168626?p={{currentPage+1}}

http://blog.csdn.net/rgb_rgb/article/details/8762769

http://blog.sina.com.cn/s/blog_87b9bbc70101avmh.html

http://stackoverflow.com/questions/13180746/mssql-connect-unable-to-connect-to-server-without-freetds-conf

http://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

http://my.oschina.net/u/217063/blog/220337

Linux環境PHP5.5以上連接SqlServer2008