A tutorial on how PHP connects SQL Server under LTNMP environment

Source: Internet
Author: User
Tags fpm ftp mssql php code stmt table name

1. Environment:

centos6.5_x64;

php5.6.11;

Tengine-2.1.0;

FreeTDS-0.95.19;

Remote sqlserver:2008;

Website Download: ftp://ftp.freetds.org/pub/freetds/stable/freetds-patched.tar.gz (as if downloading, technology Ann downloads in the time can always download to 10%, and then stopped, stopped, Stopped ... )。

This is a great effort to download the technology, to share out the download:

0.95.19:freetds-patched.tar.gz

0.95.65:freetds-patched-0.95.65.tar

2, install FreeTDS.

# TAR-ZXVF Freetds-patched.tar.gz
# CD freetds-0.95.19/
#./configure--prefix=/usr/local/freetds--with-tdsver=7.3--enable-msdblib
# make
# sudo make install
# sudo vim/etc/ld.so.conf.d/freetds.conf
Content is:/usr/local/freetds/lib/
# sudo ldconfig
Note: The installed TDS version is detailed in the official website document: http://www.freetds.org/userguide/choosingtdsprotocol.htm

Here is a screenshot:

Test whether the installation was successful:

#/usr/local/freetds/bin/tsql-c

To test the connection:

#/usr/local/freetds/bin/tsql-h Server ip/domain-P-u user name-p password
The following illustration shows that the connection has succeeded:

About freetds/etc/freetds.conf Configuration Items
Many other posts have written about the need to configure/usr/local/freetds/etc/freetds.conf, in fact this does not need to be configured. If the configuration can also be configured with PHP can call this configuration item, otherwise need to specify the PHP code database server information.
In addition to note that the/usr/local/freetds/etc/under the freetds.conf is different from the front/usr/local/freetds/lib/that freetds.conf.
If configured here, then the PHP page can use the configuration here, otherwise the PHP page can be specified as well.

This is the default:

# $Id: freetds.conf,v 1.12 2007-12-25 06:02:36 jklowden EXP $
#
# This ' file is ' 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,
# The freetds.conf manpage "man freetds.conf".
# Global settings are overridden by those in a database
# Server Specific section
[Global]
# TDS Protocol version
; TDS Version = 4.2
# Whether to write a tdsdump the file for diagnostic purposes
# (Setting this to/tmp are 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 have out-of-memory errors, it may 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 add a configuration, you can add the following code at the end:

[sqlServer2008]
host = server IP or domain name
Port = Ports
TDS Version = 7.3
3, PHP add MSSQL Extension and PDO pdo_dblib extension

# Enter the PHP source directory
# CD php-5.6.11/
# CD ext/mssql/
# phpize
#./configure--with-php-config=/usr/local/php/bin/php-config--with-mssql=/usr/local/freetds
# make
# sudo make install

# Modify the php.ini file to support MSSQL
# sudo Vim/usr/local/php/etc/php.ini
# Increase Extension
# extension=mssql.so
Add pdo_dblib Extension

# CD ext/pdo_dblib/
# phpize
#./configure--with-php-config=/usr/local/php/bin/php-config--with-pdo-dblib=/usr/local/freetds/
# make
# sudo make install
# Modify the php.ini file to support Pdo_dblib
# sudo Vim/usr/local/php/etc/php.ini
# Increase Extension
# extension=pdo_dblib.so

# Restart PHP-FPM
# sudo/etc/init.d/php-fpm Restart
4. Test PHP Call SQL Server database information

<?php
Header ("content-type:text/html; Charset=utf-8 ");

$msdb =mssql_connect ("Database Address: Port", "Account", "password");
if (! $msdb) {
echo "Connect SQL Server Error";
Exit
}

mssql_select_db ("Database name", $MSDB);
$result = Mssql_query ("SELECT Top 5 * from table name", $MSDB);
while ($row = Mssql_fetch_array ($result)) {
Var_dump ($row);
}

Mssql_free_result ($result);
?>
PDO version test:

<?php
Header ("content-type:text/html; Charset=utf-8 ");
   try {
        $hostname = "Database IP";
        $port = port;
        $dbname = "database name";
        $username = "Account";
        $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 datasheet name");
$stmt->execute ();
while ($row = $stmt->fetch ()) {
    var_dump ($row);

Related Article

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.