0

I have been thrown into the world of PHP, and I'm having a bit of an issue connecting to my SQL server. I'm using the following code to try to connect. When it hits the SQLsrv_connect command, it just seems to stop processing. No error, it just stops loading.

function testConnection()
{
    $returnable = "TEST";

    echo 'Current PHP version: ' . phpversion();
    $connectionInfo = array("Database"=>"MyDatabase", "UID"=>"MyLogin", "PWD"=>"MyPassword");
    $conn = sqlsrv_connect("MyServer",$connectionInfo);
    if ($conn) {
       echo "Connection Established.<br />";
    } else {
       echo "Something went wrong while connecting to MSSQL.<br />";
    }

     return $returnable;
}

Any idea on what I might be missing? I tried some very old syntax for version 5, and I got the same issue. I am trying to connect to sql server 2008.

Thanks

6
  • How do you connect to the database normally? Is it with Windows based authentication or do you use a username and password? Commented Jan 16, 2018 at 19:46
  • SQL authentication Commented Jan 16, 2018 at 19:48
  • It stops processing or does it echo anything? Commented Jan 16, 2018 at 19:51
  • It just stops processing. any echoes before will be displayed, but if I try putting one after, it will not be displayed. Commented Jan 16, 2018 at 19:53
  • So the if logic doesn't do anything Commented Jan 16, 2018 at 19:53

1 Answer 1

1

First, check if you have installed the sql_srv extension for PHP. Probably this extension is not installed/loaded by you php.ini file.

For Windows

Download proper driver from SQL Driver for MSSQL extract and copy into you php installation directory. Then edit php.ini file and add in extensions path your extension. (For 99% you should copy NTS sql_srv version) also don't forget add sql_srv_pdo extension.

For Ubuntu/Linux You can try install sql_srv and pdo_sql_srv by pecl (if is installed) pecl install sqlsrv pdo_sqlsrv.

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

1 Comment

I did find this issue on my local machine, and was able to get it working (I have php 7.0). However, now I am trying it on my dev server, and the connection test works fine, however, the exact same code won't work.

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.