Example of connecting to sqlserver using pdo in php

Source: Internet
Author: User

Download the PDO_DBLIB Library

Various databases of PDO can be found in PECL, for example, MySQL database: PDO_MYSQL and Oracle Database: PDO_OCI.

As the SQL Server Connection Library, run the following command to download PDO_DBLIB:

Copy codeThe Code is as follows:
Wget http://pecl.php.net/get/PDO_DBLIB

Install the PDO_DBLIB Library

After the download is complete, install it through PEAR:

Copy codeThe Code is as follows:
/Usr/bin/pear install PDO_DBLIB-1.0.tgz

If the installation is successful, the/usr/lib64/php/modules (non-64-bit hosts should have more pdo_dblib.so libraries in the/usr/lib/...) Directory (for example ). Next we need to combine the pdo_dblib.so library with php to enter/etc/php. d and create a file named pdo_dblib.ini. Write the following code:

Copy codeThe Code is as follows:
Extension = pdo_dblib.so

Restart the Apache service

Copy codeThe Code is as follows:
Service httpd restart

PHP Testing

Use a simple piece of code to test whether MSSQL can be connected normally. When using PDO to access different types of databases, you only need to modify the connection parameters in PDO () to make other calling functions the same. In this way, different operation functions are not called because of different databases during development.

Copy codeThe Code is as follows:
<? Php
$ Db = new PDO ("dblib: host = myHost; dbname = myDB", "myUserName", "myPassword ");
$ SQL = "select count (*) count from testTable ";
$ Res = $ db-> query ($ SQL );
While ($ row = $ res-> fetch ()){
Print_r ($ row );
}
$ Res = null;
$ Db = null;
?>

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.