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)
I. Closing iptables and SELinux
The code is as follows |
Copy Code |
# Service Iptables Stop
# Setenforce 0
# Vi/etc/sysconfig/selinux
---------------
Selinux=disabled
---------------
|
The code is as follows |
Copy Code |
# ntpdate Cn.pool.ntp.org
|
I. Installation of apache+php
Transmission door: HTTP://WWW.SHOWERLEE.COM/ARCHIVES/6
Two. Use FreeTDS to enable PHP connection MSSQL
The code is as follows |
Copy Code |
# Su-
# wget Ftp://ftp.freetds.org/pub/freetds/stable/freetds-stable.tgz
|
The code is as follows |
Copy Code |
# 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)
code is as follows |
copy code |
# 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)
The code is as follows |
Copy Code |
# 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
The code is as follows |
Copy Code |
# Vi/usr/local/php5/lib/php.ini
Add the following line:
Extension = "/usr/local/php5/lib/php/extensions/no-debug-zts-20090626/mssql.so"
Restart Apache:
#/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)
# 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.
The code is as follows |
Copy Code |
# vi/usr/local/apache2/htdocs/info.php
---------------
<?php
Echo Phpinfo ();
?>
|
---------------
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"
# vi/usr/local/apache2/htdocs/test_mssql_conn.php
---------------------------------------------
The code is as follows |
Copy Code |
<?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.
PostScript: If some friends use Yum installed lamp environment, then later need to add the MSSQL module to PHP.
# yum Install Php-mssql-y