0

After retrieving data from database, how do I fetch the results of the data and echo them out using mysqli? I have a couple of echos in a form and the variables which will retrieve data from the database are in those echos:

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

In the view source it is not outputting form at all.

user1914374
  • 1,220
  • 2
  • 11
  • 21

1 Answers1

1

After the code you've got there you need to use the fetch() method to loop through.

For example:

while ($sessionqrystmt->fetch()) {
    // Here you can use the bound_results variables
}
EM-Creations
  • 4,195
  • 4
  • 40
  • 56
  • Hi, I have used this but problem is that it is not echoing anything, I have no errors and query is suppose to output a result – user1914374 Feb 06 '13 at 14:26
  • @user1914374 I don't see where you're using `fetch()` can you please show us up-to-date code? – EM-Creations Feb 06 '13 at 14:43
  • @user1914374 If you try executing the exact same statement in PHPMyAdmin do you get any results? It may be that your query simply isn't returning anything. – EM-Creations Feb 06 '13 at 16:51
  • I am getting results from the query, that is a a definite – user1914374 Feb 06 '13 at 18:30
  • @user1914374 It could be that `$_POST['session']` doesn't contain any data. Could you try outputting the contents of `$_POST['session']` before doing the query? – EM-Creations Feb 07 '13 at 09:07