If you need to use PHP to connect to a Microsoft SQL Server 2005 database on a Linux system, our common way to connect to a database is to use ODBC to connect.
FreeTDS provides an open source client for the TDS protocol for Linux systems. Because MSSQL and Sybase use the TDS protocol, you can use FreeTDS to connect MSSQL in Linux, and the following Jinschau use PHP's db-lib to connect to a Microsoft SQL Server 2005 database.
Linux operating system: centOS6.3 64bit (Installation of the system default development package)
apache:httpd-2.4.4
php:php-5.4.13
freetds:freetds-0.91
First, close iptables and SELinux
Copy Code code as follows:
# Service Iptables Stop
# Setenforce 0
# Vi/etc/sysconfig/selinux
---------------
Selinux=disabled
---------------
Second, synchronization time
Copy Code code as follows:
# ntpdate Cn.pool.ntp.org
Iii. installation of apache+php
Transmission door: http://www.jb51.net/article/54969.htm
Iv. using FreeTDS to enable PHP connectivity MSSQL
1. Download FreeTDS
Copy Code code as follows:
# Su-
# wget Ftp://ftp.freetds.org/pub/freetds/stable/freetds-stable.tgz
2. Install FreeTDS
Copy Code code as follows:
# TAR-ZXVF Freetds-stable.tgz
# CD freetds-0.91
#./configure--prefix=/usr/local/freetds--with-tdsver=8.0--enable-msdblib--enable-dbmfix--with-gnu-ld-- Enable-shared--enable-static
# Make && make install
3. Enable PHP connection MSSQL
Method one: Compile PHP plus FreeTDS path (initial deployment recommended)
Copy Code code as follows:
# wget HTTP://CN2.PHP.NET/DISTRIBUTIONS/PHP-5.4.13.TAR.BZ2
# TAR-JXVF PHP-5.4.13.TAR.BZ2
# CD php-5.4.13
#./configure--prefix=/usr/local/php5--with-apxs2=/usr/local/apache2/bin/apxs--with-libxml-dir=/usr/local/lib-- With-zlib-dir=/usr/local/lib--with-mysql=/usr/local/mysql--with-mysqli=/usr/local/mysql/bin/mysql_config-- WITH-GD--enable-soap--enable-sockets--enable-xml--enable-mbstring--with-png-dir=/usr/local /local--with-curl=/usr/lib--with-freetype-dir=/usr/include/freetype2/freetype/--enable-bcmath--enable-calendar --enable-zip--enable-maintainer-zts--with-mssql=/usr/local/freetds
# Make && make install
Method Two: Compile the MSSQL Extension of PHP and add the FreeTDS path (two deployment recommendations)
Copy Code code as follows:
# wget HTTP://CN2.PHP.NET/DISTRIBUTIONS/PHP-5.4.13.TAR.BZ2
# TAR-JXVF PHP-5.4.13.TAR.BZ2
# CD Php-5.4.13/ext/mssql
#/usr/local/php/bin/phpize
#./configure--with-php-config=/usr/local/php/bin/php-config--with-mssql=/usr/local/freetds
# Make && make install
After installation, follow the instructions for compiling the installation
The mssql.so module is saved by default in the/usr/local/php5/lib/php/extensions/no-debug-zts-20090626/directory
Load the module into PHP
Copy Code code as follows:
# Vi/usr/local/php5/lib/php.ini
Add the following line:
Copy Code code as follows:
Extension = "/usr/local/php5/lib/php/extensions/no-debug-zts-20090626/mssql.so"
Restart Apache:
Copy Code code as follows:
#/usr/local/apache2/bin/apachectl Restart
Note: If the FreeTDS directory cannot be found when compiling PHP load FreeTDS, the following actions are required (official FAQ)
Copy Code code as follows:
# Touch/usr/local/freetds/include/tds.h
# TOUCH/USR/LOCAL/FREETDS/LIB/LIBTDS.A
4. Add phpinfo function, test page
Add the following info.php to/usr/local/apache2/htdocs/to test whether the PHP module is loaded.
Copy Code code as follows:
# vi/usr/local/apache2/htdocs/info.php
---------------
<?php
Echo Phpinfo ();
?>
Check if port 80 is open
Copy Code code as follows:
Browser input http://localhost/info.php
Shows the following test page content, the Mssql.so module is loaded successfully under PHP.
5. Database Connection Test page:
Assume that the MSSQL database IP is 192.168.100.10, the port is 1433, and the Administrator account password is "sa"
Copy Code code as follows:
# vi/usr/local/apache2/htdocs/test_mssql_conn.php
<?php
if (Mssql_connect (' 192.168.100.10,1433 ', ' sa ', ' sa ')) {
echo "Success";
}
else {
echo "Failed";
}
?>
Browser input http://localhost/test_mssql_conn.php
Test the final connection status.
Done....
PostScript: If some friends use Yum installed lamp environment, then later need to add the MSSQL module to PHP.
Deployment methods:
Copy Code code as follows:
# yum Install Php-mssql-y
# Service httpd Restart