PHP using PDO to connect to SQL server example sharing


Download PDO_ Dblib Library

Various PDO libraries can be found in PECL. For example, MySQL Library: PDO_ MySQL and Oracle libraries: PDO_ OCI。

As the connection Library of SQL server, Download PDO through the following command_ DBLIB:

Copy codeThe code is as follows:

Install PDO_ Dblib Library

After downloading, install through pear:

Copy codeThe code is as follows:
/usr/bin/pear install PDO_DBLIB-1.0.tgz

If the installation is successful, / usr / lib64 / PHP / modules (non 64 bit hosts should be in / usr / lib /…) There will be more PDOS in the directory_ dblib. So Library (as shown below). Next, you need to add PDO_ dblib. Combine the so library with PHP and enter / etc / PHP D and create a file named PDO_ dblib. INI file. Write the following code in it:

Copy codeThe code is as follows:

Restart Apache service

Copy codeThe code is as follows:
service httpd restart

PHP test

Test whether you can connect to MSSQL normally through a simple code. When using PDO to access different types of databases, you only need to modify the connection parameters in PDO (). Other calling functions are the same, so that different operating functions will not be called due to different databases during development.

Copy codeThe code is as follows:
$db = new PDO(“dblib:host=myHost;dbname=myDB”,”myUserName”,”myPassword”);
$sql = “select count(*) count from testTable”;
$res = $db->query($sql);
while ($row = $res->fetch()){
$res = null;
$db = null;