选择功能以便在功能之外使用

时间:2013-10-29 05:05:39

标签: php mysql pdo

我想在函数内部选择​​条目,并在函数外部的while语句中使用结果。

这就是我的......

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

我想做点像......

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

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

虽然搞乱了,但是它让它部分工作,除了它在同一行上循环的事实。

任何指针?

1 个答案:

答案 0 :(得分:1)

在您的示例中,您实际上从未使用过该语句。

$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
}