I have two classes - Gallery and Photo. The Gallery class selects a list of database ids and creates a Photo using the id. In the Gallery file the relevant code is
// fetch photos in Gallery class
private function fetchPhotos() {
$this->db = mydb::getConnection();
$statement = $this->db->prepare("SELECT DISTINCT pid FROM photogallery_link WHERE gid = :id");
$statement->execute(array(':id' => $this->id));
$pids = $statement->fetchAll();
for ($i = 0; $i < count($pids); $i++) {
$p = new Photo();
$p->fetchData($pids[$i]);
$this->photos[] = $p;
echo "\$p->fetchData($pids[$i])(from Gallery):<br />";
var_dump($p);
}
}
The values in the $pids seem to be the correct $id values. They are used to call following code in Photo:
// fetchData() function in Photo class
public function fetchData($id) {
$this->db = mydb::getConnection();
$statement = $this->db->prepare("SELECT * FROM photos WHERE photos_id = :id");
$statement->execute(array(':id' => $id));
$this->data = $statement->fetch();
// see what's going on
echo "$this->data (from Photo, select result): <br />\n";
var_dump ($this->data);
// fill the fields
$this->setId($this->data['id']);;
$this->setUrl($this->data['url']);
$this->setCaption($this->data['caption']);
$this->setDescription($this->data['description']);
$this->setFilename((pathinfo($this->url, PATHINFO_FILENAME)));
$this->setExt((pathinfo($this->url, PATHINFO_EXTENSION)));
$this->setPath(str_replace("." . $this->ext, "", $this->url));
}
The Photo code works if I create a Photo object directly like this:
$p = new Photo();
$p->fetchData(4);
$p->printThumbLink(3);
But it doesn't work when I try it from a Gallery class as in the code above. When I try it that way the var_dump in the Photo class shows bool(false), indicating the SELECT statement isn't working. I can't figure out why - suggestions welcome.