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

时间:2013-02-06 13:52:45

标签: php 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>

更新

在视图源中,根本不输出表单。

1 个答案:

答案 0 :(得分:1)

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

例如:

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