Home > Article > Backend Development > PHP uses pdo to connect to sqlserver example sharing
Download PDO_DBLIB library
Various libraries of PDO can be found in PECL, for example, MySQL library: PDO_MYSQL, Oracle library: PDO_OCI.
As the connection library of SQL Server, download PDO_DBLIB through the following command:
wget http://pecl.php.net/get/PDO_DBLIB
Install the PDO_DBLIB library
After the download is completed, install through PEAR:
/usr/bin/pear install PDO_DBLIB-1.0.tgz
If If the installation is successful, there will be an additional pdo_dblib.so library in the /usr/lib64/php/modules (non-64-bit hosts should be in /usr/lib/...) directory (as shown below). Next, you need to combine the pdo_dblib.so library with php, enter /etc/php.d and create a file named pdo_dblib.ini. Write the following code in it:
extension=pdo_dblib.so
Restart the Apache service
service httpd restart
PHP test
Test whether MSSQL can be connected normally through a simple code. When using PDO to access different types of databases, you only need to modify the connection parameters in PDO() and the other calling functions will be the same, so that different operating functions will not be called due to different databases during development.
<?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; ?>
For more php using pdo to connect to sqlserver examples and related articles, please pay attention to the PHP Chinese website!