Home  >  Article  >  Backend Development  >  PHP uses pdo to connect to sqlserver example sharing

PHP uses pdo to connect to sqlserver example sharing

高洛峰
高洛峰Original
2017-01-06 14:26:252576browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn