title: PDO query class
body: <p>I'm tinkering with a class that 'should' allow me to easily execute a fetchall query and display the results within a foreach statement. I assume all is working correctly as I have no errors. As for the foreach - this must be the problem? How would I foreach the results gained from the <code>$connect-&gt;query()</code>? I'm new to using any database OOP framework in my functions so I could be along the wrong lines completely.</p>
  
  <pre><code> &lt;?
  
  error_reporting(1);
  
  class dbconnect {
  
  private $host;
  private $database;
  private $username;
  private $password;
  
  private $pdo;
  private $error;
  
      public function __construct() {
  
          $this-&gt;host     = "localhost";              // Host
          $this-&gt;database = "images";                 // Database Name
          $this-&gt;username = "*";              // Username
          $this-&gt;password = "*";  // Password
          $options  = array(
              PDO::MYSQL_ATTR_INIT_COMMAND =&gt; 'SET NAMES utf8'
          );
  
          try {
              $this-&gt;pdo = new PDO("mysql:host={$this-&gt;host};dbname={$this-&gt;dbname};charset=utf8", $this-&gt;username, $this-&gt;password, $options);
          }
          catch(PDOException $e) {
              $this-&gt;error = $e-&gt;getMessage();
          }
  
      }
  
      public function query($query) {
  
          try {
              $stmt = $this-&gt;pdo-&gt;prepare($query);
              $stmt-&gt;execute(); 
          } catch(PDOException $ex) { 
              die("Failed to run query: " . $ex-&gt;getMessage()); 
          } 
  
          $rows = $stmt-&gt;fetchAll();
          return $rows;
  
      }
  
  }
  
  $connect = new dbconnect;
  $rows = $connect-&gt;query("select * from photos");
  foreach($rows as $row):
      print $row['id'];
  endforeach;
  
  
  
  ?&gt;
  </code></pre>