mysqli 准备在 table 中不返回结果

mysqli prepare not returning results in table

我正在尝试 post 从准备好的语句返回的每一行作为自定义格式的单独 table 行 - 不幸的是,我过去在准备好的语句方面取得的成绩很差,我想将其写入当前标准-

下面的代码不会将任何行回显到 table。

感谢任何帮助

$link = mysqli_connect("host", "user", "password", "db");
if ($stmt = mysqli_prepare($link, "SELECT * FROM users WHERE currentemp=1")) {
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $result);
mysqli_stmt_fetch($stmt);
foreach ($result as $row) {
//print data
echo("<tr><td><input type='checkbox' name='emp". $row['UID'] ."'></td><td>". $row['Fname'] ." ". $row['Lname'] ."</td><td>". $row['hiredate'] ."</td><td>" .$row['certlevel'] ."</td><td>". $row['email'] ."</td><td>". $row['phonenumb'] ."</td></tr>");
}

} else {
echo(" <tr><td colspan=5>No employees found</td></tr>");
}
mysqli_stmt_close($stmt);

注意,根据the documentationmysqli_stmt_bind_result() "binds columns in the result set to variables."

mysqli_fetch()只有returns一个结果行。您需要使用 while 循环遍历结果集。

$link = mysqli_connect("localhost", "root", "Ddsmp094", "dmt"); 

if ($stmt = mysqli_prepare($link, "SELECT UID, Fname, Lname, hiredate, certlevel, email, phonenumb FROM users WHERE currentemp=1")) {
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $UID, $Fname, $Lname, $hiredate, $certlevel, $email, $phonenumb);
while(mysqli_stmt_fetch($stmt)) {
    //print data
    echo("<tr><td><input type='checkbox' name='emp". $UID ."'></td><td>". $Fname ." ". $Lname ."</td><td>". $hiredate ."</td><td>" .$certlevel ."</td><td>". $email ."</td><td>". $phonenumb ."</td></tr>");
}
} else {
echo(" <tr><td colspan=5>No employees found</td></tr>");
}
mysqli_stmt_close($stmt);