Connect to SQL Server through PDO

Source: Internet
Author: User

In the development of PHPProgramYou can use multiple connection methods to access various databases to obtain the required data. Since PhP5, PDO has included all database interfaces as new things, providing developers with a convenient and fast way to read databases. This article describes how to configure the connection between PHP and SQL server on a Linux server.

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:

Wget http://pecl.php.net/get/PDO_DBLIB

Install the pdo_dblib Library

After the download is complete, install it through pear:

/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 followingCode:

Extension = pdo_dblib.so

Restart the apache service

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.

 
<? PHP $ db =NewPDO ("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.