title: Connecting to SQL Server 2012 on IIS8 with PHP_SQLSRV
body: <p>This is a project, so doesn't require extreme levels of caution/security - I'd like to get it working and then improve it from there.</p>
  
  <h2>How can I get PHP to connect successfully to SQL Server 2012</h2>
  
  <p>What I have done successfully:</p>
  
  <ul>
  <li>Enabled mixed authentication on SQL Server 2012</li>
  <li>Established an account using SQL authentication</li>
  <li>Set up FastCGI on IIS and configured PHP.ini</li>
  <li>.PHP scripts in the web root execute successfully</li>
  <li>I can use SSMS to connect locally to the SQL database using the credentials (SQL Authentication) I created</li>
  </ul>
  
  <p>The problem:</p>
  
  <ul>
  <li>I cannot use a PHP script to successfully connect to the local SQL database.</li>
  </ul>
  
  <p>What I have considered, but do not understand well enough to diagnose:</p>
  
  <ul>
  <li>That the HTTP connections to IIS are impersonating IUSR, which has permissions to read/execute PHP scripts in the web root, but may not have permission to connect to the server?</li>
  <li>That for some reason my connection dialogue is simply ineffective</li>
  </ul>
  
  <p>Here is my connect script:</p>
  
  <pre><code>$serverName = 'localhost';
  $connectionInfo = array( 'Database'=&gt;'DATABASE',
                         'Encrypt'=&gt;'1', 'UID'=&gt;'user', 'PWD'=&gt;'password' );
  
  $connection = sqlsrv_connect($serverName, $connectionInfo);
  
  if( $connection == false )
  {
    echo "No connection was established.\n";
    die( print_r( sqlsrv_errors(), true));
  }
  </code></pre>
  
  <p>Here are my errors (unfortunately PHP does not pretty print them):</p>
  
  <pre><code>No connection was established. Array (
      [0] =&gt; Array (
          [SQLSTATE] =&gt; 08001
          [code] =&gt; -2146893019
          [message] =&gt; [Microsoft][SQL Server Native Client 11.0]SSL Provider: The certificate chain was issued by an authority that is not trusted.
      )
      [1] =&gt; Array (
          [SQLSTATE] =&gt; 08001
          [code] =&gt; -2146893019
          [message] =&gt; [Microsoft][SQL Server Native Client 11.0]Client unable to establish connection
      )
  ) 
  </code></pre>
  
  <p>I also tried using process monitor to check w3wp.exe when I load the script through a browser - I don't know how I would paste the information from that, but there doesn't seem to be anything wrong with its output.</p>