In the development of PHPProgramYou can use multiple connection methods to access various databases to obtain the required data. Since PhP5, PDO has included all database interfaces as new things, providing developers with a convenient and fast way to read databases. This article describes how to configure the connection between PHP and SQL server on a Linux server.
Download the pdo_dblib Library
Various databases of PdO can be found in PECL, for example, MySQL database: pdo_mysql and Oracle Database: pdo_oci.
As the SQL Server Connection Library, run the following command to download pdo_dblib:
Wget http://pecl.php.net/get/PDO_DBLIB
Install the pdo_dblib Library
After the download is complete, install it through pear:
/Usr/bin/pear install PDO_DBLIB-1.0.tgz
If the installation is successful, the/usr/lib64/PHP/modules (non-64-bit hosts should have more pdo_dblib.so libraries in the/usr/lib/...) Directory (for example ). Next we need to combine the pdo_dblib.so library with PHP to enter/etc/PHP. D and create a file named pdo_dblib.ini. Write the followingCode:
Extension = pdo_dblib.so
Restart the apache service
Service httpd restart
PHP Testing
Use a simple piece of code to test whether MSSQL can be connected normally. When using PDO to access different types of databases, you only need to modify the connection parameters in PDO () to make other calling functions the same. In this way, different operation functions are not called because of different databases during development.
<? PHP $ db =NewPDO ("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;?>