2

I Have a big problem And I Wish get Solved for this problem here I Try to Connect to Sql Server 2008 R2 using:

  1. php 5.6.11
  2. Xampp 5.6.3
  3. Sql Server 2008 r2
  4. Using sql server drivers for PHP (php_pdo_sqlsrv_56_ts.dl & php_sqlsrv_56_ts.dll)

and my code to try connection is :

 $serverName = "Mahmoud-HP\SQL2008R2"; //serverName\instanceName
    $connectionInfo = array( "Database"=>"HR16", "UID"=>"Mahmoud", "PWD"=>"123" , "MultipleActiveResultSets" => false);
    $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));
    }

but when run the page this message come to me

Fatal error: Call to undefined function sqlsrv_connect() in C:\xampp\htdocs\testsqlserver\index.php on

What is solution for this problem

5
  • 3
    Create a php page with phpinfo(). That should show you if your SQL server drivers are correctly being loaded. Commented Mar 16, 2016 at 12:33
  • jszobody how i Show it in this page ?? what is flag ??!! can you give me more explain Commented Mar 16, 2016 at 12:45
  • undefined function clearly means the extension is not installed and your description of how you installed is basically "Using". In any case, I suggest you run PHP from the command line so you have a chance to see initialisation errors. Just php -v should trigger them. Commented Mar 16, 2016 at 13:02
  • @PERSON Fire a plain text editor (not a word processor), type <?php phpinfo(); in it, save it as C:\xampp\htdocs\testsqlserver\phpinfo.php and then load it in your browser (it should be the same URL you are already using, except that ending with phpinfo.php). Commented Mar 16, 2016 at 13:09
  • Did you restart your apache after editing the php.ini? Commented Mar 16, 2016 at 13:17

1 Answer 1

2

you have to use the SQL Server native driver for php.

Download from here:

https://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=20098

Example Code:

$serverName = "tcp:ServerID.database.windows.net, 1433";

$connectionOptions = array("Database" => "DatabaseName", 

                           "UID" => "Username@ServerID",

                           "PWD" => "password");

$conn = sqlsrv_connect($serverName, $connectionOptions);



if($conn === false)

{

    die(print_r(sqlsrv_errors(), true));

}

EDIT

Make sure you load BOTH dll's listed:

extension=php_sqlsrv_56_ts.dll

and

extension=php_pdo_sqlsrv_56_ts.dll
Sign up to request clarification or add additional context in comments.

3 Comments

Native Driver is necessary for the extension to actually work, but you need to install the extension first and the OP hasn't (thus the undefined function error message).
@Álvaro González you're in right. I have edited my answer
this works well on local (osx) but won't on windows server 2012, I don't know why yet

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.