-1

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.

Shane
  • 305
  • 6
  • 16
  • There is no `select()` function in PDO. If your code not working, you are supposed to debug it yourself, not ask someone else to do it for you. – Your Common Sense Oct 21 '13 at 10:46
  • @Your Common Sense - I am using PDO prepare() with a select query inside. This, as far as I can tell, is the correct way to do it e.g. http://stackoverflow.com/questions/767026/how-can-i-properly-use-a-pdo-object-for-a-select-query – Shane Oct 21 '13 at 10:52
  • Yes, exactly. But your 'Calling a PDO SELECT function' is incorrect wording. – Your Common Sense Oct 21 '13 at 11:09
  • question title edited... – Shane Oct 21 '13 at 11:20

1 Answers1

0

There is absolutely no difference, where you create your class - within a function, or in a global scope, or other class' method. There is even no variable scope issue (as long as you're not using freshly created objects somewhere else).

So, your problem is somewhere else. You have to debug your code to find out the place where something goes wrong and why class creation getting no proper data.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345