Limey
Limey

Reputation: 2772

PHP can't connect to SQL Server

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

Upvotes: 0

Views: 756

Answers (1)

bliszka
bliszka

Reputation: 68

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.

Upvotes: 1

Related Questions