: download "Microsoft Drivers 3.0 for PHP for SQL Server" from http://www.microsoft.com/en-us/download/details.aspx?id=20098
question 1: SQLSRV20.EXE or SQLSRV30.EXE
answer1: SQL Server 2008 series --> SQLSRV20.EXE; SQL Server 2012 series --> SQLSRV30.EXE
for me, I installed SQL Server 2008, so i must use SQLSRV20.EXE
step 2: unpack it and read "SQLSRV_Help.chm" --> "Getting Started" --> "Loading the Microsoft Drivers for PHP for SQL Server"
for me, I prefer "Loading Driver at PHP Startup", so I add three lines in php.ini:
extension=php_sqlsrv_53_ts_vc9.dll
extension=php_pdo_sqlsrv_53_ts_vc9.dll
extension=php_pdo.dll
question 2: what is php_pdo.dll and where should I get php_pdo.dll
answer 2: php_pdo.dll is a pdo driver to load sqlsrv, then see step3
step 3: download php_pdo.dll from http://www.dlldll.com/php_pdo.dll_download.html
step 4: move necessary dll files to where they belong
for me, it is "C:\Program Files (x86)\PHP\ext"
step 5: restart IIS
for me, it is activating IIS Manager and restart my "default web site"
step 6: write first testing php program (info.php) to check if "sqlsrv" is enabled.
test program: info.php
phpinfo(); ?> |
step 7
: write second testing php program (db0.php) to check if it can access SQL Server
test program: db0.php
$host = "(local)"; $database = "testDB"; try { $conn = new PDO( "sqlsrv:server=$host;Database=$database;"); $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); } catch( PDOException $e ) { //die( "Error connecting to SQL Server" ); die( print_r( $e->getMessage() ) ); } echo "Connected to SQL Server\n"; $query = 'select * from cmsdata'; $stmt = $conn->query( $query ); while ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) ){ print_r( $row ); } ?> |
You should get result like this:
C:\inetpub\wwwroot\scripts\sql>php db0.php Connected to SQL Server Array ( [filename] => Wildlife.wmv [city] => Taipei [macaddr] => F0:DE:F1:78:77:92 [datetime] => 2012-09-25 15:30:00.0000000 ) C:\inetpub\wwwroot\scripts\sql> |