我正在尝试使用while循环来回显出属于一个帐号的所有学生ID。当我删除while循环时,下面给出的代码执行正常,但它没有使用while循环输出,并且浏览器页面继续加载。这是代码:
include "database.php";
$q = $_REQUEST["student_id"];
$q = strtolower($q);
$trimmed = trim($q);
$query4 = "select student_id from acc_details where account_no= (select account_no from acc_details where student_id = $trimmed)";
while ( $arr1 = mysqli_fetch_assoc(mysqli_query($con, $query4))) {
extract($arr1);
echo "<h1> $student_id </h1>";
// I want to echo out some html output here
}
答案 0 :(得分:3)
<强>问题:强>
它没有输出while循环,浏览器页面继续加载。
<强>解决方案:强>
请仔细阅读以下声明,
while ( $arr1 = mysqli_fetch_assoc(mysqli_query($con, $query4))) {
...
}
问题是,在while循环的每次迭代中,您都在执行查询,这就是为什么while循环继续执行,它永远不会结束。在while循环之外执行查询并将结果集循环遍历它,如下所示:
include "database.php";
$q = $_REQUEST["student_id"];
$q = strtolower($q);
$trimmed = trim($q);
$query4 = "select student_id from acc_details where account_no IN (select account_no from acc_details where student_id = {$trimmed})";
$result=mysqli_query($con,$query4);
while ($row = mysqli_fetch_assoc($result)) {
// your code
}