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.
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:
$bookings[id] => booking Object ( [name:protected] => name, [time:protected] => time )
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:
class create_bookings {
private $db;
protected $error;
function __construct($db, $current, $end) {
$this->db = $db;
$query = "SELECT id, name, time
FROM bookings";
$stmt = $this->db->prepare($query);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_CLASS, 'booking');
print_r($result);
}
}
...and the 'booking' class is just a set of properties:
class booking {
private $db;
protected $name;
protected $time;
}