emsoff
emsoff

Reputation: 1605

Select in function for use outside of function

I'd like to select entries inside of a function and use the results in a while statement outside of the function.

This is what I have...

public function getPractices($dbh) {
    $practices = $dbh->prepare("SELECT * FROM `practices`");
    $practices->execute();
    return $practices;
    }

I would like to then do something like...

$practices = new SomethingDumbthing;
$practices->getPractices($dbh);

while ($row = $practices->fetch(PDO::FETCH_ASSOC)) { 
    Do stuff
}

While messing around, I got it to partially work, except for the fact that it was looping over the same row.

Any pointers?

Upvotes: 1

Views: 38

Answers (1)

elixenide
elixenide

Reputation: 44831

In your example, you never actually use the statement.

$practices = new Class;

// this returns an object, but you don't save it to anything!
// try $data = $practices->getPractices($dbh);
$practices->getPractices($dbh);

// so now, you are calling fetch on your class, not on the database results!
while ($row = $practices->fetch(PDO::FETCH_ASSOC)) { 
    Do stuff
}

Upvotes: 1

Related Questions