PHP uses PDO to connect SQL Server samples to share _mssql

Source: Internet
Author: User
Tags pear

Download Pdo_dblib Library

PDO's various libraries can be found in pecl, for example, MYSQL Library: Pdo_mysql, Oracle Library: Pdo_oci.

As a connection library for SQL Server, download pdo_dblib with the following command:

Copy Code code as follows:

wget Http://pecl.php.net/get/PDO_DBLIB

Installing the Pdo_dblib Library

After the download is complete, install through Pear:

Copy Code code as follows:

/usr/bin/pear Install Pdo_dblib-1.0.tgz

If the installation is successful,/usr/lib64/php/modules (non 64-bit host should be in/usr/lib/... The Pdo_dblib.so library (pictured below) is listed in the directory. The following need to combine the pdo_dblib.so Library with PHP, enter the/ETC/PHP.D and create a file named Pdo_dblib.ini. Write the following code in it:

Copy Code code as follows:

Extension=pdo_dblib.so

Restart the Apache service

Copy Code code as follows:

Service httpd Restart

PHP test

Use a simple code to test whether you can connect MSSQL properly. When using PDO for different types of database access, you only need to modify the connection parameters in PDO () to be the same as other calling functions, so that different action functions will not be invoked when the database is developed.

Copy Code code as follows:

<?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;
?>

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.