title: PHP - PDO SQLSRV - Connection string is not valid [87]
body: <p>I´m trying to connect to a SQL Server 2008 R2 via PHP. I used the following code:</p>
  
  <pre><code>try {
      $server = "servername.do.ma.in\\MSSQLSERVER";
      $user = "username";
      $pass = "password";
      $db = "databasename";
  
      $dbh= new PDO('sqlsrv:Server = ' . $server . '; Database = ' . $db, $user, $pass);
  
      $dbh-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  } catch (PDOException $e) {
      echo 'Verbindung fehlgeschlagen: ' . $e-&gt;getMessage();
  }
  </code></pre>
  
  <p>But when i run this code i get the following error:</p>
  
  <pre><code> SQLSTATE[08001]: [Microsoft][ODBC Driver 11 for SQL Server]SQL Server Network Interfaces: Connection string is not valid [87]. 
  </code></pre>
  
  <p>Does someone know this error and know what to do? </p>
  
  <p>Thanks in advance.</p>
  
  <hr>
  
  <p><strong>Solution:</strong></p>
  
  <p>The Port was not defined. The following code worked for me.
  I´ve added the Port 1433 to the $server Variable.</p>
  
  <pre><code>$server = "servername.do.ma.in, 1433";
      $user = "username";
      $pass = "password";
      $db = "databasename";
  
      $dbh = new PDO("sqlsrv:Server={$server};Database={$db};", $user, $pass);
  </code></pre>