title: DBLIB PDO driver keeps failing when trying to connect/query Microsoft Azure SQL database
body: <p>So I'm using Linux (ubuntu) and I'm trying to connect to a Microsoft Azure SQL database.</p>
  
  <p>Server information:</p>
  
  <ul>
  <li><p>The server address is a1a1a1a1a1.database.windows.net (a1a1a1a1a1 is a placeholder for my server name)</p></li>
  <li><p>The database name is MyDatabase</p></li>
  <li><p>The table is called [dbo].[Sloth]</p></li>
  </ul>
  
  <p>Freetds.conf settings:</p>
  
  <pre><code>[global]
      dump file = /tmp/freetds.log
      debug flags = 0xffff
      text size = 64512
  
  [a1a1a1a1a1.database.windows.net]
      host = a1a1a1a1a1.database.windows.net
      port = 1433
      tds version = 8.0
      client charset = UTF-8
  </code></pre>
  
  <p>When I run </p>
  
  <pre><code>php -r "phpinfo();" | grep "PDO drivers"
  </code></pre>
  
  <p>in terminal it returns</p>
  
  <pre><code>PDO drivers =&gt; dblib, mysql
  </code></pre>
  
  <p>so to the best of my knowledge all the configuration and driver installations are how they should be.</p>
  
  <p>So now for the errors:</p>
  
  <p><strong>ERROR 1</strong></p>
  
  <p>If I initialize the PDO like so:</p>
  
  <pre><code>$conn = new \PDO ( "dblib:dbname = MyDatabase;host=a1a1a1a1a1.database.windows.net;", $Username, $Password);
  </code></pre>
  
  <p>it connects, but if I remove the spaces around the '=' character for dbname like so:</p>
  
  <pre><code>$conn = new \PDO ( "dblib:dbname=MyDatabase;host=a1a1a1a1a1.database.windows.net;", $Username, $Password);
  </code></pre>
  
  <p>it returns these errors from the PDOException and freetds.log </p>
  
  <pre><code>SQLSTATE[HY000] General SQL Server error: Check messages from the SQL Server (severity 16)
  
  (dbutil.c:86):msgno 40508: "USE statement is not supported to switch between databases. Use a new connection to connect to a different Database."
  </code></pre>
  
  <p><strong>ERROR 2</strong></p>
  
  <p>When I run the following code</p>
  
  <pre><code>try {
      $conn = new \PDO ( "dblib:dbname = MyDatabase;host=a1a1a1a1a1.database.windows.net;", $Username, $Password);
      $conn-&gt;setAttribute( \PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION );
      $statement = $conn-&gt;prepare("SELECT * FROM dbo.Sloth");
      $result = $statement-&gt;execute();
  }
  catch ( PDOException $e ) {
      print( "Error connecting to SQL Server." );
      die(print_r($e));
  }
  </code></pre>
  
  <p>it breaks at the $result = ... line with these errors from the PDOException and freetds.log </p>
  
  <pre><code>SQLSTATE[HY000]: General error: 208 General SQL Server error: Check messages from the SQL Server [208] (severity 16) [(null)] 
  
  (dbutil.c:86):msgno 208: "Invalid object name 'dbo.Sloth'."
  </code></pre>
  
  <p>but when I run the query</p>
  
  <pre><code>SELECT * FROM dbo.Sloth
  </code></pre>
  
  <p>in the Azure Database Management Portal it returns all the correct rows.</p>
  
  <p>Any help with these issues will be much appreciated!</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Through some tests I've discovered that both problems stem from the fact that the PDO isn't connecting to the database but is instead connecting to the "master". This would explain ERROR 2's Invalid Object Name error. </p>
  
  <p>Any thoughts as to why the code in ERROR 1 doesn't connect to MyDatabase?</p>