FreeTDS configuration for connecting to SQL Server based on PHP in Linux

Source: Internet
Author: User

In CentOS 5.4 Linux, PHP (FastCGI) needs to connect to the SQL Server 2000 database of the relevant department and configure the extended FreeTDS extension.

1. Compile and install 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.  
  10. echo "/usr/local/webserver/freetds/lib/" > /etc/ld.so.conf.d/freetds.conf  
  11. ln -s /usr/local/webserver/freetds/lib/libsybdb.so.5.0.0 /usr/local/webserver/freetds/lib/libsybdb.so.4  
  12. /sbin/ldconfig  
  13.  
  14. rm -f /usr/local/webserver/freetds/etc/freetds.conf  
  15. vi /usr/local/webserver/freetds/etc/freetds.conf  

Enter the following content:

Reference

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

2. Compile and install PHP's built-in MSSQL Extension

Enter the local php-5.2.XX source package directory that already exists:

 
 
  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. add mssql. so to the php. ini configuration file.

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

Add a row:

Reference

 
 
  1. extension = "mssql.so" 

4. Restart PHP FastCGI.

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

5. Test file (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. ?>    


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.