0
votes

I am trying to connect to SQL Server 2008 on machine-1 from PHP code on another machine-2. I am using PHP 5.4.7, XAMPP 1.8.1 and I have copied the sql server dlls into PHP/ext folder and modified the php.ini file.

Now, when I try to connect to SQL server I am getting following error.

Array ( [0] => Array ( [0] => IMSSP [SQLSTATE] => IMSSP [1] => -49 [code] => -49 [2] => This extension requires the Microsoft SQL Server 2012 Native Client. Access the following URL to download the Microsoft SQL Server 2012 Native Client ODBC driver for x86: http://go.microsoft.com/fwlink/?LinkId=163712 [message] => This extension requires the Microsoft SQL Server 2012 Native Client. Access the following URL to download the Microsoft SQL Server 2012 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 ) )

My PHP code is as follows -

<?php
//phpinfo(); 
$server = "sql server\express,1433"; //serverName\instanceName, portNumber (default is 1433)
$uid="username";
$pwd="password";
$connectionInfo = array("UID" => $uid, "PWD" => $pwd, "Database"=>"rod_prd_tmart");
$conn = sqlsrv_connect( $server, $connectionInfo) ;

if($conn) 
{
   echo "Connection established.\n";
} else
{
   echo "Connection could not be established in this text.\n";
   die( print_r( sqlsrv_errors(), true));
}

sqlsrv_close( $conn);
?>

Please help me resolve this issue. Thanks.

1
download and install the driver from go.microsoft.com/fwlink/?LinkId=163712 and edit php.ini. copying sql server dlls don't work - bansi
Thanks Bansi.Actually I have downloaded and installed the sql server drivers and edited the php.ini file alrdy. - user2742601
have you also restarted your web server? your code looks fine - bansi
Tip: print_r() output's has carriage returns. You'll see them if you use the "View Source" feature of your browser. - Álvaro González
I suppose you've already done it but you don't say it explicitly. Have you installed Microsoft SQL Server 2012 Native Client? - Álvaro González

1 Answers

0
votes

Consider using PDO for more flexibility:

//$pdo = new PDO("sqlsrv:Server=$hostname;Database=$dbname;", $username, $password);  // works with proper driver for PHP.
$pdo = new PDO("odbc:Driver={SQL Server};Server=$hostname;Database=$dbname;", $username, $password);  // works with proper driver for ODBC and PHP ODBC.

I could not get the first line to work due to weird compiler version incompatibilities, but the second one worked fine after installing Microsoft ODBC Driver 11 for SQL Server

PHP Version 5.3.0 has built-in ODBC support, according to php.ini, but here that still lists an active extension=php_pdo_odbc.dll