PHP uses pdo to connect to sqlserver example sharing

高洛峰
Release: 2023-03-04 16:16:02
Original
2560 people have browsed it

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
Copy after login

Install the PDO_DBLIB library

After the download is completed, install through PEAR:

/usr/bin/pear install PDO_DBLIB-1.0.tgz
Copy after login

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
Copy after login

Restart the Apache service

service httpd restart
Copy after login

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;
?>
Copy after login

For more php using pdo to connect to sqlserver examples and related articles, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!