gpt4 book ai didi

php - 在函数内选择在函数外使用

转载 作者:行者123 更新时间:2023-11-29 00:24:22 24 4
gpt4 key购买 nike

我想在函数内部选择​​条目并在函数外部的 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
}

虽然四处乱逛,但我让它部分工作,除了它在同一行上循环。

有什么指点吗?

最佳答案

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

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

关于php - 在函数内选择在函数外使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19650075/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com