4

I'm attempting to connect to a 2005 Microsoft sql database using PHP by using the sqlsrv_connect() function. alas, the function returns false, and im not sure why.

<?php
$myServer = "MAZE.jpc.wa.edu.au.local";
$myUser = "myUsername";
$myPass = "myPassword";
$myDB = "John Paul College";
$connectionInfo = array("Database"=>$myDB, "UID" => $myUser, "PWD" => $myPass);

$conn = sqlsrv_connect($myServer, $connectionInfo); //returns false
if( $conn === false )
{
    echo "failed connection";
}

$sql = "SELECT name FROM users WHERE name= 'admin'";
$stmt = sqlsrv_query($conn,$sql);
if(sqlsrv_fetch($stmt) ===false)
{
    echo "couldn't fetch data"; 
}
$name = sqlsrv_get_field($stmt,0);
echo $name;
sqlsrv_close( $conn );
?>

Does anyone know why I can't connect? Thanks.

Edit.

Ok, well, i was able to bring up an error message thanks to the other guys answer, which states

Array (
    [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 ) )

I'm not entirely sure how to go about fixing this issue. I'm using XAMPP as my test webserver, php version 5.3 and the following .dlls php_sqlsrv_53_ts_vc6.dll and php_pdo_sqlsrv_53_ts_vc6.dll

7
  • 1
    What is the exact error you're getting? Commented Dec 5, 2014 at 3:13
  • is your database named John Paul College? with all those spaces? then i think you should be using $myDB = "[John Paul College]"; Commented Dec 5, 2014 at 3:15
  • You need to download and install the drivers like the error message says. It pretty much explains how to solve your problem ? Commented Dec 5, 2014 at 3:56
  • doesn't it clearly stated you need SQL Server 2008 or higher? If you are still using SQL server 2005 try using mssql Commented Dec 5, 2014 at 3:57
  • Unfortunately I don't have the option of upgrading the server. I'm stuck with 2005. Does anyone know what drivers I should be using if im currently using the wrong ones? Commented Dec 5, 2014 at 4:02

2 Answers 2

4

I would suggest that you display the connection error using sqlsrv_errors(). Please see the following implementation.

<?php
   $serverName = "serverName\sqlexpress"; //serverName\instanceName

   // Since UID and PWD are not specified in the $connectionInfo array,
   // The connection will be attempted using Windows Authentication.
   $connectionInfo = array( "Database"=>"dbName");
   $conn = sqlsrv_connect( $serverName, $connectionInfo);

   if( $conn ) {
     echo "Connection established.<br />";
   }else{
     echo "Connection could not be established.<br />";
     die( print_r( sqlsrv_errors(), true));
   }
?>

For more info: http://php.net/manual/en/function.sqlsrv-connect.php

Sign up to request clarification or add additional context in comments.

Comments

1

Download the SQL Native Client as the error message suggests. This is separate from SQL Server and a requirement for this driver to work correctly. Lesson learned from building an implementation of sqlsrv in an IIS environment

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.