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.

1 Answer 1

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
}
Sign up to request clarification or add additional context in comments.

5 Comments

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 I don't see where you're using fetch() can you please show us up-to-date code?
@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.
I am getting results from the query, that is a a definite
@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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.