How to connect MSSQL using PHP under Linux

Source: Internet
Author: User
Tags server port

Catalogue [-]

    • How to install mssql.so
    • How to install Pdo_lib. So
    • How to install PDO_ODBC. So

Author: Roban Lee ([email protected])

There are many ways to connect MSSQL using PHP under Linux, depending on the environment, you can choose different ways, roughly the following methods:

    • Using mssql.so extensions
    • Using Pdo_lib extensions
    • Using PDO_ODBC extensions

How to install mssql.so

1. MSSQL. So extension relies on an external package, namely FreeTDS, FreeTDS official website: http://www.freetds.org. After downloading the latest installation package from the official website, install (Specify a directory) by executing the following command:

?
1 ./configure--prefix=/usr/local/freetds

2. Configure FreeTDS

Open the freetds.conf in the FreeTDS ECT directory, and add the following at the end of the file:

?
1234 [testServer]host = 192.168.x.x   #你的SQL SERVER IPport = 1433          #SQL SERVER PORTtds version = 7.0

3. Go to the Ext directory in the PHP source directory, find the MSSQL directory, compile the extension

?
1 ./configure--with-php-config=/usr/local/php/bin/php-config--with-mssql=/usr/local/freetds

4. After the installation is complete, execute the following command to add the extension to the PHP configuration file

?
1 echoextension=mssql.so >> PHP_INSTALL_PATH/lib/php.ini

5. See if the extension is already loaded.

?
1 php -m | grepmssql

6. Write a script to test for normal loading (Mssql_connect the first parameter is the server name in the FreeTDS you just configured)

?
123 <?phpmssql_connect(‘testServer‘,‘SQL SIGNIN ACCOUNT‘, ‘PASSWORD‘) OR DIE (‘cannot connect msserver‘)

7. Done

How to install Pdo_lib. so

Pdo_lib installation method and dependent package basic and MSSQL the same, the only difference is the compile time parameters, enter the Pdo_lib expansion package, the installation time parameter input:

?
1 ./configure--with-php-config=/usr/local/php/bin/php-config--with-pdo-dblib=/usr/local/freetds

After the installation is complete, add pdo_lib.so to the configuration file and then write the test file:

?
1234 <?php$dbh= newPDO("dblib:host=testServer;dbname=db","test","test");var_dump($dbh);

How to install PDO_ODBC. sopdo_odbc This extension is slightly different from the above 2 extensions, depending on 2 packages, one FreeTDS, the other is UNIXODBC, there are some differences in the installation method, the first is the need to install UNIXODBC (: http://www.unixodbc.org/)

After download, install directly after installation, you need to add a configuration, in the UNIXODBC/ETC directory to add the following configuration:

?
1234567 [testDSN]Driver          = /usr/local/freetds/lib/libtdsodbc.soDescription      = First Test DSNTrace           = NoServername      = testServer   # 这个就是FREETDS里面配置的服务器信息Database        = testUID             = test

Next is the installation of FREETDS requires an additional parameter:

?
1 ./configure--prefix=/usr/local/freetds--with-unixodbc=/usr/local/unixODBC

After installing FreeTDS, go to the extension directory to install the extension:

?
1 ./configure--with-php-config=/usr/local/php/bin/php-config--with-pdo-odbc=unixODBC,/usr/local/unixODBC

After the installation is complete, write a script to test the connection:

?
123456 <?php$dbh = new PDO(‘odbc:testDSN‘,‘test‘,‘test‘); var_dump( $dbh);

All is done.

Good LUCK,

How to connect MSSQL using PHP under Linux

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.