Linux Build install MSSQL client and configure PHP connection MSSQL

Source: Internet
Author: User
Tags install php mssql mssql client stmt

Generally connected to the database, you must install the client, I generally do not write blog writing principles, but later found to look back to their own writing I do not understand, so this chapter blog will be a bit more original rational things.


Database connections are generally done on the client, MySQL is no exception, when installing MySQL is generally installed:

#yum install MySQL Mysql-server

The above MySQL is the installation of the client, Mysql-server is the server, do not install the client (MySQL) will not be able to execute the MySQL connection command: mysql-u user-p pass-h-P Port

and SQL Server also needs the client, the following is the installation of SQL Server in Linux under the client FreeTDS


Installation Environment:

#centos6.8#php5.6#sqlserver2008


    1. Download FreeTDS, the general will be downloaded on the official website, this open source presumably no one will use piracy.


wget ftp://ftp.freetds.org/pub/freetds/stable/freetds-patched.tar.gz#linux download is a bit slow and can be downloaded on Windows and then uploaded to Linux. Tar zxf freetds-patched.tar.gzcd freetds-1.00.27./configure--prefix=/usr/local/freetds--with-tdsver=7.3-- Enable-msdblibmake && make Install


2. Import Ldconfig

echo "/usr/local/freetds/lib/" >/etc/ld.so.conf.d/freetds.conf ldconfig


3. View the installed version:

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

4. Test if you can connect to SQL Server:

/usr/local/freetds/bin/tsql-h 10.10.10.10-p 1433-u sa-p Password

5. Add the PHP extension MSSQL and Pdo_dblib, this is the PHP two way to connect MSSQL, only install MSSQL can also.

Note: The general installation of PHP will choose two ways, one is to compile the installation, one is the Yum installation. Here are the two ways to install the method:

1) Yum-installed PHP

#很简单, directly Yun install the MSSQL plug-in can:

Yum Install Php-pdo-yyum Install php-mssql-y

2) compiled and installed PHP

#进入到php的安装包中, install the MSSQL plugin
Cd/home/tools/php-5.6.20/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


#安装pdo_dblibcd/home/tools/php-5.6.20/ext/pdo_dblib//usr/local/php/bin/phpize./configure--with-php-config=/usr/ Local/php/bin/php-config--with-pdo-dblib=/usr/local/freetds/make && make install

Add MSSQL and Pdo_dblib to php.ini:

mv/usr/local/php/lib/php/extensions/no*/mssql/usr/local/php/lib/php/extensions/mv/usr/local/php/lib/php/ extensions/no*/pdo_dblib/usr/local/php/lib/php/extensions/#把前面安装的扩展插件生成的文件移动到extension目录下vi/usr/local/php/lib/ Php.iniextension_dir = "/usr/local/php/lib/php/extensions" extension = "mssql.so" extension = "pdo_dblib.so"


3) Restart PHP to see if the extension is in effect:

#编译一个php测试文件:

Vi/www/html/test.php<?phpphpinfo ();? >

Open the above test file with your browser and you will see the following message, which indicates a successful installation:

http://ip/test.php

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M01/95/E2/wKiom1kauemDpP72AAC6lkTfzxw811.jpg "title=" Sogou May 16, 17 1635_1.jpg "alt=" Wkiom1kauemdpp72aac6lktfzxw811.jpg "/>


6. Test the MSSQL Connection sqlserver2008:

#编辑一个测试文件:

vi/www/html/connect_sql.php<?php header ("content-type:text/html;    Charset=utf-8 ");  $msdb =mssql_connect ("10.10.10.10:1433", "sa", "password");      if (! $msdb) {echo "Connect SQL Server Error";      Exit  } mssql_select_db ("Test", $MSDB);  $result = Mssql_query ("SELECT Top 5 * from student", $MSDB);  while ($row = Mssql_fetch_array ($result)) {Print_r ($row);  } mssql_free_result ($result); ?>

#如果内容是sqlserver the test database, the contents of the student table are successfully connected to the database on behalf of PHP.


7. Test Pdo_dblib Connection sqlserver2008:

#创建一个测试文件vi  /www/html/connect_db.php<?php  header ("content-type: text/html;  Charset=utf-8 ");    try {       $hostname  = " 10.10.10.10 ";       $port  = 1433;      $ dbname =  "Test";       $username  =  "sa";        $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 student ");     $stmt->execute ();    while  ($ row =  $stmt->fetch ())  {      print_r ($row);     }    unset ($DBH);  unset ($stmt);    ?>

#如果内容是sqlserver the test database, the contents of the student table are successfully connected to the database on behalf of PHP.


This article from "Wang Jiadong elder brother" blog, declined reprint!

Linux Build install MSSQL client and configure PHP connection MSSQL

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.