【发布时间】:2019-09-12 21:30:24
【问题描述】:
我正在尝试使用 PHP 循环遍历 MySQL 表,但它只显示一行。
//Retrieve a list of outstanding developments
$sql = "SELECT * FROM tblDevelopment WHERE strStatus=?";
$statement = mysqli_stmt_init($conn);
//Check for any errors in the SQL statement
if (!mysqli_stmt_prepare($statement,$sql)){
//Report any errors with the prepared $statement
header("Location: ../sqlerror.php");
exit();
} else {
//If there are no errors, query the database for the username
mysqli_stmt_bind_param($statement,'s', $status);
mysqli_stmt_execute($statement);
$results = mysqli_stmt_get_result($statement);
if ($row = mysqli_fetch_assoc($results)) {
echo 'header';
while ($row = mysqli_fetch_assoc($results))
{
echo $row['strDetail'] . "</";
}
echo 'footer';
} else {
echo 'No results to display';
}
}
代码在没有结果时有效,但当有多个结果时它只显示一个结果 - 知道我做错了什么吗?
【问题讨论】:
-
if 是抓取一条记录。然后是抓住另一个。检查 mysqli_num_rows 以查看有多少记录受到影响。
-
感谢您的回答 - 我已经取出了 if 位,只是使用了 while,它似乎工作正常。
标签: php mysqli mysqli-fetch-array