Linux version: 64-bit CentOS 6.4
Nginx Version: nginx1.8.0
PHP Version: php5.5.28
SQL Server version: 2008
FreeTDS version: 0.95
For Linux environment Installation nginx+php reference Linux environment Nginx installation and debugging as well as PHP installation.
In general, Php+mysql is the most classic combination, running in a Linux environment is very good if Php+sqlserver is running in the Windows environment.
Today, the Linux environment requires PHP to call SQL Server, using a day, and finally put this problem thoroughly studied, the other similar articles on the internet I mostly see, in fact, there are some because too long do not apply, some have errors, there are several key issues not clear, see this article other can be ignored, Said really step on the pit really tired, there is no need, according to this article to do is, so this wencai claims to be the most classic error-free version of the network, in fact, so the main hope that you save time.
1. First you need to compile and install FreeTDS
Note: Be sure to download the latest version from the official website FreeTDS-0.95 ftp://ftp.freetds.org/pub/freetds/stable/freetds-patched.tar.gz
If the official website is too slow suggestions from I uploaded here as soon as download: 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
Note that this is the--with-tdsver=7.3 here, this is very important, you need to choose the correct configuration according to your database version, because now mostly SQLserve2008 so need to choose 7.3.
About this question online some say is 7.1, also some say is 7.2, even some say is 8.0, can see the text at the end of reference post, but those said all have problems.
The cause of this configuration confusion is that many people use FreeTDS-0.91, after my test FreeTDS-0.91 only support 7.1, if it is more than 7.2 configuration then all will become 5.0.
In fact, refer to the official website of the document to know this problem, but because many people download the old version of FreeTDS-0.91, even if set to--with-tdsver=7.2 above also no use.
Summary:FreeTDS-0.91 only supports 7.1, the rest will default to 5.0. Only the latest FreeTDS-0.95, which is the best configuration for Sqlserver2008.
#./configure--prefix=/usr/local/freetds--with-tdsver=7.3--enable-msdblib
# Make && make install
Install and you will see this message:
Configure FreeTDS
# CD: /
# echo "/usr/local/freetds/lib/" >/etc/ld.so.conf.d/freetds.conf
# Ldconfig
Verifying the FreeTDS version
This step is very important, it can be continued, or the subsequent steps are meaningless.
First look at the version information
# /usr/local/freetds/bin/tsql-c
Test Database Connectivity
# /usr/local/freetds/bin/tsql-h Database Server ip-p port number-u user name-p password
About freetds/etc/freetds.conf Configuration Items
Many other posts have been written to configure/usr/local/freetds/etc/freetds.conf, in fact this does not need to be configured . If the configuration is also possible, the configuration of PHP can invoke this configuration item, otherwise you need to specify the database server information in the PHP code.
It is also important to note that the freetds.conf under the/usr/local/freetds/etc/is different from the freetds.conf in front/usr/local/freetds/lib/.
If configured here, then the PHP page can use the configuration here, or PHP page designation is the same.
The default is this:
[CPP]View Plain Copy
- # $Id: freetds.conf,v 1.12 2007-12-25 06:02:36 jklowden EXP $
- #
- # This file was installed by FreeTDS if no file by the same
- # name is found in the installation directory.
- #
- # For information about the layout of this file and its settings,
- # See the freetds.conf manpage ' man freetds.conf '.
- # Global settings is overridden by those in a database
- # Server Specific section
- [Global]
- # TDS Protocol version
- ; TDS Version = 4.2
- # Whether to write a tdsdump file for diagnostic purposes
- # (Setting this to/tmp was insecure on a multi-user system)
- ; dump file =/tmp/freetds.log
- ; Debug flags = 0xFFFF
- # Command and Connection Timeouts
- ; Timeout = 10
- ; Connect Timeout = 10
- # If you get out-of-memory errors, it could mean that your client
- # is trying to allocate a huge buffer for a TEXT field.
- # Try Setting ' text Size ' to a more reasonable limit
- Text size = 64512
- # A Typical Sybase server
- [EgServer50]
- Host = symachine.domain.com
- Port = 5000
- TDS Version = 5.0
- # A Typical Microsoft server
- [EgServer70]
- Host = ntmachine.domain.com
- Port = 1433
- TDS Version = 7.0
If you want to use a configuration item, simply modify [EgServer70] to:
[CPP]View Plain Copy
- [EgServer70]
- Host = 192.168.1.235 This is the database server IP
- Port = 1433
- TDS Version = 7.1
Other do not move, about [EgServer70] 's name is also arbitrary, this is to PHP call, and PHP code consistent.
3. Add PHP extensions for MSSQL and PDO Pdo_dblib
Note: These 2 kinds of extensions can achieve the same purpose, choose one.
(1). Increase the PHP extension MSSQL
# cd/usr/php-5.5.28/ext/mssql/
Using Phpize to add extensions to PHP dynamically under Linux
# /usr/local/php/bin/phpize
# ./configure--with-php-config=/usr/local/php/bin/php-config--with-mssql=/usr/local/freetds/
# make && make install
(2). Increase the pdo_dblib of the PHP extension PDO
# cd/usr/php-5.5.28/ext/pdo_dblib/
Using Phpize to add extensions to PHP dynamically under Linux
# /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). Add. So in the php.ini configuration file
# PHP.ini under the Cd/usr/local/php/lib
Increase:
[CPP]View Plain Copy
- Extension = "mssql.so"
- Extension = "pdo_dblib.so"
If you only need one of the 2 extensions above, naturally just add one of the. so extension to php.ini.
(4). Restart PHP FastCGI
# Killall PHP-FPM
#/ETC/INIT.D/PHP-FPM
The PHP-FPM cannot be restarted if the extension is not properly generated.
This time in the phpinfo can see the extension to add success information.
4. Calling SQL Server using PHP
(1). Mssql_connect Configuration Version
[PHP]View Plain Copy
- <?php
- Header ("content-type:text/html; Charset=utf-8 ");
- $msdb =mssql_connect ("EgServer70", "blog.csdn.net.unix21", "password");
- if (! $msdb) {
- echo "Connect SQL Server Error";
- Exit
- }
- mssql_select_db ("Database name", $MSDB);
- $result = Mssql_query ("SELECT Top 5 * from TableName", $MSDB);
- while ($row = Mssql_fetch_array ($result)) {
- Print_r ($row);
- }
- Mssql_free_result ($result);
- ?>
Note: The above egServer70 is the previous freetds/etc/freetds.conf configuration.
(2). Mssql_connect Non-configurable version
[PHP]View Plain Copy
- <?php
- Header ("content-type:text/html; Charset=utf-8 ");
- $msdb =mssql_connect ("Database IP", "blog.csdn.net.unix21", "password");
- $msdb =mssql_connect ("Database ip:1433", "blog.csdn.net.unix21", "password");
- $msdb =mssql_connect ("Database ip:49151", "blog.csdn.net.unix21", "password");
- if (! $msdb) {
- echo "Connect SQL Server Error";
- Exit
- }
- mssql_select_db ("Database name", $MSDB);
- $result = Mssql_query ("SELECT Top 5 * from TableName", $MSDB);
- while ($row = Mssql_fetch_array ($result)) {
- Print_r ($row);
- }
- Mssql_free_result ($result);
- ?>
(3). PDO version
[PHP]View Plain Copy
- <?php
- Header ("content-type:text/html; Charset=utf-8 ");
- try {
- $hostname = "Database IP";
- $port = 1433;
- $dbname = "database name";
- $username = "blog.csdn.net.unix21";
- $PW = "password";
- $DBH = new PDO ("dblib:host= $hostname: $port;d bname= $dbname", "$username", "$PW");
- } catch (Pdoexception $e) {
- echo "Failed to get DB handle:". $e->getmessage (). "\ n";
- Exit
- }
- $stmt = $dbh->prepare ("SELECT Top 5 * from tablename");
- $stmt->execute ();
- while ($row = $stmt->fetch ()) {
- Print_r ($row);
- }
- Unset ($DBH); Unset ($stmt);
- ?>
Show Data:
Above I have been verified.
Reference Information :
1. This post explains the first important question why--with-tdsver=7.1
http://blog.csdn.net/dlutxie/article/details/6851429
2. Official information
http://php.net/manual/zh/ref.pdo-dblib.php
http://php.net/manual/zh/function.mssql-connect.php
3. I used to refer to these, but there are mistakes in some places, the Internet is mainly about these posts, others are quoted from each other.
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/[email protected]/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 environment PHP5.5 above connection SqlServer2008