Page 1 of 1

Access an instance of PDO from another class

Posted: Wed Jul 14, 2010 6:20 am
by steel_rose
Hi everybody,

I'm trying to build something that, I believe, is very common, but I'm not finding many up to date resources online and I was wondering if anyone could point me in the right direction.

I have a database of people and I would like to create a class (e.g. 'person') that displays the information about the person but that also can upload and update those info.
I'm accessing the database via PDO, and I tried to write a code like this:

Code: Select all

class volontario {

    private $name;
    private $surname;
    private $mobile;
    private $landline;
    private $license;
    private $notes;

    public function loadVol($id){

        $loadQuery = "SELECT * FROM volontari WHERE id=$id";
        $dbh->loadQuery->execute($loadQuery);

    }

        $dbh = dbconn::getInstance();
        $id = 17;
       $io = new volontario();
       $io->loadVol($id);
But PHP kindly replied this:

[text]
Fatal error: Call to a member function execute() on a non-object in C:\xampp\htdocs\crocerossa\inc\lib.php on line 69
[/text]
I suppose the main issue is that I have is that I'm trying to access an object ($dbh, my PDO) from inside another object?

Any suggestion regarding how I should approach this or links to examples will be very appreciated!


Thanks,

SR