是的,我不确定如何提出这个问题。
让我试着举例说明我想在这里做些什么。
function getRecents()
{
global $sql;
$buffer = $sql->prepare("SELECT * FROM `foo` WHERE `exposure` = 'public' ORDER BY `id` DESC LIMIT 5");
$buffer->execute();
return $buffer->fetch(PDO::FETCH_OBJ);
}
while($pd = getRecents())
{
echo $pd->id;
}
就像我下面要做的那样。很明显,因为我不止一次地使用它,所以我不会把它扔到这个地方。
$buffer = $sql->prepare("SELECT * FROM `foo` WHERE `exposure` = 'public' ORDER BY `id` DESC LIMIT 5");
$buffer->execute();
while($foo = $buffer->fetch(PDO::FETCH_OBJ))
echo $foo->id;
我认为有人会理解我想要做的事情,如果有的话请我很困惑。 我会谷歌但不确定如何。
答案 0 :(得分:1)
我想你应该尝试这样的事情:
function getRecents()
{
global $sql;
$buffer = $sql->prepare('SELECT * FROM foo WHERE exposure = "public" ORDER BY id DESC LIMIT 5');
$buffer->execute();
return $buffer->fetchAll(PDO::FETCH_OBJ);
}
$recents = getRecents();
foreach ($recents as $item) {
echo $item->id;
}
答案 1 :(得分:0)
如果存在一组对象/数组,则foreach循环会更好。
$buffer = $sql->prepare('SELECT * FROM foo WHERE exposure = "public" ORDER BY id DESC LIMIT 5');
$buffer->execute();
foreach($buffer->fetchAll(PDO::FETCH_OBJ) as $row) {
print_r($row);
}