gpt4 book ai didi

PHP 获取数据两次

转载 作者:可可西里 更新时间:2023-11-01 07:48:20 24 4
gpt4 key购买 nike

我的函数看起来像那样。

 private function generateTree($courseID) {
$q = "SELECT l.id, l.name AS lesson_name, c.name AS course_name FROM lessons AS l, courses AS c WHERE l.course_id=c.id AND c.id=?";
$stmt = $this->db->prepare($q);
$stmt->bind_param("i", $courseID);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows > 0) {
$stmt->bind_result($id, $lName, $cName);
echo "<li> <a href='#'>$cName</a> <ul>";
while ($stmt->fetch()) <====HERE!!!
echo "<li> <a href='?course=$courseID&lesson=$id'> $lName </a></li>";
echo "</ul> </li>";
}
}

问题是我开始在 while 条件内获取数据,但在 while 之前我也需要它。我可以两次获取数据吗?还有其他建议吗?

最佳答案

已经四年了,但如果有人像我一样偶然发现这个问题:

while ($stmt->fetch()) {
// do your thing
}

$stmt->data_seek(0);

while ($stmt->fetch()) {
// do something other
}

您可以 read documentation here

关于PHP 获取数据两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11526867/

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