Php – Sqlsrv_connect() error

PHP

I'm trying to connect PHP 5.3.5 to MS Sql Server 2005. I'm using Windows XP SP3. I enabled the sqlsrv function which is also shown in phpinfo(). I also made some changes in the php.ini extension but I keep getting following error:

"Database connection errorArray ( [0] => Array ( [0] => IMSSP
[SQLSTATE] => IMSSP [1] => -49 [code] => -49 [2] => This extension
requires either the Microsoft SQL Server 2008 Native Client (SP1 or
later) or the Microsoft SQL Server 2008 R2 Native Client ODBC Driver
to communicate with SQL Server. Neither of those ODBC Drivers are
currently installed. Access the following URL to download the
Microsoft SQL Server 2008 R2 Native Client ODBC driver for x86:
http://go.microsoft.com/fwlink/?LinkId=163712 [message] => This
extension requires either the Microsoft SQL Server 2008 Native Client
(SP1 or later) or the Microsoft SQL Server 2008 R2 Native Client ODBC
Driver to communicate with SQL Server. Neither of those ODBC Drivers
are currently installed. Access the following URL to download the
Microsoft SQL Server 2008 R2 Native Client ODBC driver for x86:
http://go.microsoft.com/fwlink/?LinkId=163712 ) [1] => Array ( [0] =>
IM002 [SQLSTATE] => IM002 [1] => 0 [code] => 0 [2] => [Microsoft][ODBC
Driver Manager] Data source name not found and no default driver
specified [message] => [Microsoft][ODBC Driver Manager] Data source
name not found and no default driver specified ) ) 1"

I've installed sqlncli.msi , SQLSRV20.exe and SQLSRV30.exe but I'm still getting the same error.

My program to connect:

<?php
$server = "Patrik/SQLEXPRESS";
$conInfo = array("Database"=>"ShoppingDetail","UID"=>"Pat1890","PWD"=>"Pat1890");

$con = sqlsrv_connect($server,$conInfo);

if($con==false)
 {
  echo "Error Connecting to database<br>";
   die (print_r(sqlsrv_errors()));
  }
else
{
  echo "Database connection is sucessfully eastablished";
}
?> 

I think there is no problem in my coding. I also enabled TCP/IP and used the IP address instead of servername but the problem still persists.

Best Answer

Fisrt verify, in php.ini

extension=php_pdo_mssql.dll

and

extension=php_pdo_odbc.dll

and try with PDO

$server = 'host';
$database = 'database';
$username = 'username';
$password = 'password';
$db = new PDO('odbc:Driver={SQL Server}; Server=' . $server . '; Database=' . $database . '; Uid=' . $username . '; Pwd=' . $password . ';');
$query = $db->prepare('...');
$query->execute();