首页 文章

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

提问于
浏览
0

从数据库中检索数据后,如何获取数据结果并使用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>

UPDATE:

<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>

UPDATE:

在视图源中,它根本不输出形式 .

1 回答

  • 1

    在您到达的代码之后,您需要使用 fetch() 方法循环 .

    例如:

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

相关问题