Background:
PHP program commonly used database is MySQL database, but due to the actual project needs, the PHP Web site to connect to the SQL Server database to query some necessary information. Therefore, this article will introduce you how to install and configure the PHP extension, you can implement PHP to use the PDO function to connect to the SQL Server database.
Body:
To have PHP configured to support SQL Server, you need to install some necessary software and extensions, as follows:
1, in the official Microsoft page https://www.microsoft.com/en-us/download/details.aspx?id=20098 download PHP extension package program, PHP5.4 above version download SQLSRV32. EXE this file;
2, will download the SQLSRV32. EXE installed into your PHP installation directory in the Ext file (for example: C:\php\php-5.4.45\ext), install the extension name such as Php_pdo_sqlsrv_54_ts.dll file;
3, modify the php.ini file, we installed the PHP version is 5.4.45, so the configuration extension such as Extension = Php_pdo_sqlsrv_54_ts.dll, save and restart the Apache server;
4. You need to install a Windows Server connection to SQL Server driver, http://www.microsoft.com/en-us/download/details.aspx?id= 36434, according to the WINDWOS server architecture selection, here choose x64 Msodbcsql.msi installation, because the server is a 64-bit system;
5. Once the installation is complete, you can write a PHP program to connect to the SQL Server database.
It is important to note that the php_pdo_sqlsrv_54_ Ts.dll 54 in the Extension library file represents the PHP version number, NTS represents a non-thread-safe, TS represents thread-safe, can see phpinfo information to get thread-safe, if not sure the recommended choice of thread-safe, non-thread security mainly for performance improvement, can be based on actual needs to choose. Simply put, run PHP as a fastcgi gateway to install non-thread-safe, or install PHP to be thread-safe, the general Apache server is to load the PHP thread module mode, to choose Thread-safe.
Finally, we give a code instance of PHP connection to SQL Server database, and create a new db.php file with the following contents:
<?php
Try { $dbName= "Sqlsrv:server=123.12.33.99,1433;database=dbname"; $dbUser= "User"; $dbPassword= "Password"; $db=NewPDO ($dbName,$dbUser,$dbPassword); if($db) { Echo"Database connect succeed."; } } Catch(pdoexception$e) { $content=$e-GetMessage (); Echo $content; }
Browser address input db.php Access address: http://localhost/db.php, if normal, display the following:
Database connect succeed.
Windows 2008 Server environment PHP connection SQL Server database configuration and connection method