title: DEBUG PDO Connection & PHP Output
body: <p>I am trying to connect to mySQL using PDO.</p>
  
  <p>Please forgive me if I have made a glaring error - I am just learining...</p>
  
  <pre><code> &lt;?php
  
  try {
      $db_conn = new PDO('mysql:host=localhost;dbname=testdatabase','test', 'testpass');  
  } 
  catch (PDOException $e) {
      echo 'Could not connect to database';
  }
  
  $stmt = $db_conn-&gt;query('SELECT * FROM PRODUCTS');
  
  while ($row = $stmt-&gt;fetch() ) {
      echo '&lt;pre&gt;'; print_r($row); echo '&lt;pre&gt;';
  }
  
  ?&gt;
  </code></pre>
  
  <p>the output from the browser is as follows:</p>
  
  <pre><code>query('SELECT * FROM PRODUCTS'); while ($row = $stmt-&gt;fetch() ) { echo '
  '; print_r($row); echo '
  ';
  }
  
  ?&gt;
  </code></pre>
  
  <p>What have I done wrong??? why is PHP not parsing the PHP script?</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>If I create a new php file, and run phpinfo(); it works.</p>
  
  <p>If I paste phpinfo() into the top of the above code as follows:</p>
  
  <pre><code>&lt;?php
  
  phpinfo();
  
  echo '&lt;h1&gt;PDO TEST&lt;/h1&gt;';
  
  try {
      $db_conn = new PDO('mysql:host=localhost;dbname=testdatabase','test', 'testpass');  
  } 
  catch (PDOException $e) {
      echo 'Could not connect to database';
  }
  
  $stmt = $db_conn-&gt;query('SELECT * FROM Products');
  
  while ($row = $stmt-&gt;fetch() ) {
      echo '&lt;pre&gt;'; print_r($row); echo '&lt;pre&gt;';
  }
  
  ?&gt;
  </code></pre>
  
  <p>I get the following output:</p>
  
  <pre><code>PDO TEST'; try { $db_conn = new PDO('mysql:host=localhost;dbname=testdatabase','test', 'testpass');  } catch (PDOException $e) { echo 'Could not connect to database'; } $stmt = $db_conn-&gt;query('SELECT * FROM Products'); while ($row = $stmt-&gt;fetch() ) { echo '
  '; print_r($row); echo '
  ';
  }
  
  ?&gt;
  </code></pre>
  
  <p>UPDATE:
  Problem solved... It was some kind of file encoding issue. It works perfectly when I copy and paste the code into a new file. Very strange.</p>