【发布时间】:2022-01-25 08:57:51
【问题描述】:
我有一个工作结果集,它在运行时循环显示作者和链接列表。工作正常。我需要能够在同一页面中显示相同的数据集两次。我宁愿不要两次运行相同的查询。如何将结果集(如下所示的显示输出)设置为 var,并在两个表示点获得相同的输出?前段时间我看到了一些关于使用 for each 循环的内容,但不知道该怎么做。
查询:
if($stmtr = mysqli_prepare($link, $sql8)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmtr, "s", $param_stripurlslash);
// Set parameters
$param_stripurlslash = $stripurlslash;
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmtr)){
$resultAuthor = mysqli_stmt_get_result($stmtr);
$usera = mysqli_fetch_assoc($resultAuthor, MYSQLI_ASSOC);
} else {
echo "Oops! Something went wrong. Please try again later.";
}
}
显示输出:
<?php
while ($row = mysqli_fetch_assoc($resultAuthor)) {
if ($row['book_author_first_name_only'] == 0 ) {
echo "<a class='fw-bold author-link' href='#" . $row['book_id'] . "'>" . $row['book_author_first_name'] . ' ' . $row['book_author_last_name'] . "</a>";
} else {
echo "<a class='fw-bold author-link' href='#" . $row['book_id'] . "'>" . $row['book_author_first_name'] . "</a>";
}
}
?>
【问题讨论】:
-
试试
mysql_data_seek($resultAuthor, 0);。 How can I loop through a MySQL result set more than once using the mysql_* functions? -
是的,这个解决方案也很有效。谢谢!