欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

基于Linux下PHP连接SQL Server的FreeTDS配置

程序员文章站 2022-04-20 22:13:47
...
CentOS 5.4 Linux 下的 PHP(FastCGI) 需要连接相关部门的SQL Server 2000数据库,配置了扩展FreeTDS扩展。

1、编译安装FreeTDS

  1. mkdir -p /data0/software/
  2. cd /data0/software/
  3. wget ftp://ftp.ibiblio.org/pub/Linux/ALPHA/freetds/stable/freetds-stable.tgz
  4. tar zxvf freetds-stable.tgz
  5. cd freetds-0.82/
  6. ./configure --prefix=/usr/local/webserver/freetds --with-tdsver=8.0 --enable-msdblib
  7. make && make install
  8. cd ../
  9. echo "/usr/local/webserver/freetds/lib/" > /etc/ld.so.conf.d/freetds.conf
  10. ln -s /usr/local/webserver/freetds/lib/libsybdb.so.5.0.0 /usr/local/webserver/freetds/lib/libsybdb.so.4
  11. /sbin/ldconfig
  12. rm -f /usr/local/webserver/freetds/etc/freetds.conf
  13. vi /usr/local/webserver/freetds/etc/freetds.conf

输入以下内容:

引用

  1. [global]
  2. # TDS protocol version
  3. ; tds version = 4.2
  4. # Whether to write a TDSDUMP file for diagnostic purposes
  5. # (setting this to /tmp is insecure on a multi-user system)
  6. ; dump file = /tmp/freetds.log
  7. ; debug flags = 0xffff
  8. # Command and connection timeouts
  9. ; timeout = 10
  10. ; connect timeout = 10
  11. # If you get out-of-memory errors, it may mean that your client
  12. # is trying to allocate a huge buffer for a TEXT field.
  13. # Try setting 'text size' to a more reasonable limit
  14. text size = 64512
  15. host = mssql.yourdomain.com
  16. port = 1433
  17. tds version = 8.0
  18. client charset = UTF-8

2、编译安装PHP自带MSSQL扩展

进入本地已存在的php-5.2.XX源码包目录:

  1. cd /data0/software/php-5.2.XX/ext/mssql/
  2. /usr/local/webserver/php/bin/phpize
  3. ./configure --with-php-config=/usr/local/webserver/php/bin/php-config --with-mssql=/usr/local/webserver/freetds/
  4. make && make install

3、在php.ini配置文件中增加mssql.so

  1. vi /usr/local/webserver/php/etc/php.ini

增加一行:

引用

  1. extension = "mssql.so"

4、重启PHP FastCGI

  1. /usr/local/webserver/php/sbin/php-fpm restart

5、测试文件(test_mssql.php):

  1. php
  2. header("Content-type: text/html; charset=utf-8");
  3. $msdb=mssql_connect("mssql.yourdomain.com:1433","username","password");
  4. if (!$msdb) {
  5. echo "connect sqlserver error";
  6. exit;
  7. }
  8. mssql_select_db("database_name",$msdb);
  9. $result = mssql_query("SELECT top 5 * FROM table", $msdb);
  10. while($row = mssql_fetch_array($result)) {
  11. var_dump($row);
  12. }
  13. mssql_free_result($result);
  14. ?>