Note that there are some explanatory texts on larger screens.

plurals
  1. POConnecting to MS SQL server from PHP
    primarykey
    data
    text
    <p>I am trying to connect to a SQL Server database from PHP but I'm having problems with the connection string. Here is a rundown of what I've tried and the effect it has had:</p> <p>Running PHP 5.3.13, Apache 2.2.22, Windows 7, and SQL Server 2008 R2</p> <p>I've installed SQL Server Native Client 11.0</p> <p>I've dropped using MSSQL driver, using the SQLSRV driver instead.</p> <p>Loaded these extensions in php.ini: extension=php_pdo_sqlsrv_53_ts.dll, extension=php_sqlsrv_53_ts.dll, extension=php_pdo.dll.</p> <p>phpinfo() shows these as active:</p> <pre><code>Registered PHP Streams php, file, glob, data, http, ftp, zip, compress.zlib, phar, sqlsrv PDO drivers mysql, odbc, sqlite, sqlsrv sqlsrv support enabled </code></pre> <p>Connection code is as follows:</p> <pre><code>$serverName = "[servername]"; $connectionInfo = array( "Database"=&gt;"[databasename]"); $conn = sqlsrv_connect( $serverName, $connectionInfo); if( $conn ) { echo "Connection established.\n"; } else { echo "Connection could not be established.\n"; die( print_r( sqlsrv_errors(), true)); } </code></pre> <p>The string is set up to use Windows authentication, which sqlsrv_errors() reports:</p> <pre><code>[message] =&gt; [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user '[networkname]\[machinename]' </code></pre> <p>I have also tried this using my network id/pwd, resulting in</p> <pre><code>[message] =&gt; [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user '[username]' </code></pre> <p>I do know that the database is up and functional, as I can connect and run queries without problem from the SQL Server client, and from sqlcmd. The SQL Client and the command line work when using my network/machine credentials. The command line client fails when I try with username/password. This would indicate that any PHP connection should use the Windows authentication to connect.</p> <p>Someone in another thread suggested that changing permissions in a registry entry would work, but this did not help my issue. <a href="https://stackoverflow.com/questions/9824162/php-5-3-not-recognizing-native-client-to-connect-to-ms-sql">PHP 5.3 not recognizing Native Client to connect to MS SQL</a></p> <p>I also had some success creating a system DSN. The connection test reports success:</p> <pre><code>Microsoft SQL Server Native Client Version 11.00.2100 Running connectivity tests... Attempting connection Connection established Verifying option settings INFO: A network alias was found for the DSN server. 'Protocol: DBMSSOCN; Address: [servername]' was used to establish the connection. Disconnecting from server TESTS COMPLETED SUCCESSFULLY! </code></pre> <p>I've searched extensively within stackoverflow.com, and the interwebs in general with no luck. Obviously the db connection is alive using other methods, but what is going wrong in my connection via PHP?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload