title: PDO FETCH_CLASS multiple rows into array of objects
body: <p>I'm trying to fetch multiple rows of bookings from a database and I want each one to be an instance of a specific class - so I attempted to store them within a multidimensional array.</p>
  
  <p>So far it works in terms of creating the array of objects, however I need the index for the array to be the booking ID so that I can easily access each one. For example:  </p>
  
  <pre><code>$bookings[id] =&gt; booking Object ( [name:protected] =&gt; name, [time:protected] =&gt; time )
  </code></pre>
  
  <p>Is this possible and is it the best way to go about what I want to achieve? Any help would be hugely appreciated, here's the code:</p>
  
  <pre><code>class create_bookings {
  
  
      private $db;
    protected $error;
  
  
  
  
  function __construct($db, $current, $end) {
  
      $this-&gt;db = $db;
  
  
      $query = "SELECT id, name, time             
                FROM bookings";
  
      $stmt = $this-&gt;db-&gt;prepare($query); 
  
      $stmt-&gt;execute(); 
  
      $result = $stmt-&gt;fetchAll(PDO::FETCH_CLASS, 'booking');   
  
      print_r($result);
  
  }
  
  }
  </code></pre>
  
  <p>...and the 'booking' class is just a set of properties:</p>
  
  <pre><code>class booking {
  
        private $db;
  
      protected $name;
      protected $time;
  
  
  }
  </code></pre>