Wednesday, October 23, 2013

Steps for Connecting MS SQL 2008 and 2008 R2 Server by PHP 5.3.x Connect MS SQL 2008 R2 with PDO

Step 1. in php.ini add the following line:
extension=php_pdo_sqlsrv_53_ts_vc6.dll

Step 2. un-comment this line:
;extension=php_pdo_mssql.dll

Step 3. download php_pdo_sqlsrv_53_ts_vc6.dll from here and place it in /php/ext

Step 4. restart apache http server


Verifying the setting is correct:

$dsn       = 'mysql:host=localhost;dbname=myDB';
$login     = 'myLogin';
$passwd = 'myPassword';

$db = new PDO($dsn, $login, $passwd);
$sqlToRun = "
   SELECT
   name AS [employeeName]
   FROM
   employee
";
$res = $db->prepare($sqlToRun); 
$res->execute(); 
$res->setFetchMode(PDO::FETCH_LAZY); 
while($row = $res->fetch(PDO::FETCH_ASSOC)) {
echo $row['employeeName'];
}