如何使用mysqli从查询中检索数据


How to retrieve data from query using mysqli

从数据库中检索数据后,如何获取数据的结果并使用mysqli将其回显?我在一个表格中有几个回波,从数据库中检索数据的变量在这些回波中:

   <?php
$session = isset($_POST['session']) ? $_POST['session'] : '';
$sessionquery = "
SELECT s.SessionId, SessionName, SessionDuration, SessionDate, SessionTime, TotalMarks, SessionWeight, 
PenaltyEnabled, s.ModuleId, ModuleNo, ModuleName, StudentId
FROM Penalty p
INNER JOIN Session s ON p.SessionId = s.SessionId
INNER JOIN Module m ON s.ModuleId = m.ModuleId
LEFT JOIN Student_Session ss ON s.SessionId = ss.SessionId
WHERE
(s.SessionId = ?)
";
$sessionqrystmt=$mysqli->prepare($sessionquery);
// You only need to call bind_param once
$sessionqrystmt->bind_param("i",$session);
// get result and assign variables (prefix with db)
$sessionqrystmt->execute(); 
$sessionqrystmt->bind_result($dbSessionId,$dbSessionName, $dbSessionDuration, $dbSessionDate, $dbSessionTime, $dbTotalMarks, $dbSessionWeight, 
$dbPenaltyEnabled, $dbModuleId, $dbModuleNo, $dbModuleName, $dbStudentId);
$sessionqrystmt->store_result();
?>
<form action='results.php' method='post' id='exam'>
 <?php 
while ($sessionqrystmt->fetch()) {
echo "<p><input type='text' id='studentId' name='studentId' value='$dbStudentId' /></p>";
echo "<p>Module: " . $dbModuleNo . " - " . $dbModuleName . "<input type='text' id='moduleId' name='moduleId' value='$dbModuleId' /></p>";
echo "<p>Assessment: " . $dbSessionName . " - " . date('d-m-Y',strtotime($dbSessionDate)) . " - " . date('H:i',strtotime($dbSessionTime)) . "<input type='text' id='sessionId' name='sessionId' value='$dbSessionId' /></p>";
?>
</form>

更新:

<form action='results.php' method='post' id='exam'>
         <?php 
        while ($sessionqrystmt->fetch()) {
        echo "<p><input type='text' id='studentId' name='studentId' value='$dbStudentId' /></p>";
        echo "<p>Module: " . $dbModuleNo . " - " . $dbModuleName . "<input type='text' id='moduleId' name='moduleId' value='$dbModuleId' /></p>";
        echo "<p>Assessment: " . $dbSessionName . " - " . date('d-m-Y',strtotime($dbSessionDate)) . " - " . date('H:i',strtotime($dbSessionTime)) . "<input type='text' id='sessionId' name='sessionId' value='$dbSessionId' /></p>";
    }
        ?>
        </form>

更新:

在视图源中,它根本没有输出表单。

代码完成后,需要使用fetch()方法进行循环。

例如:

while ($sessionqrystmt->fetch()) {
    // Here you can use the bound_results variables
}