While ($ stmt-> fetch ()) does not retrieve all rows

I am trying to display all recipes belonging to a registered user using a while loop. I noticed that let them say that the user has 4 recipes, the first of them will not be displayed, which will lead to the appearance of only 3 recipes in the table. If the user has 3 recipes, the first of them will not be displayed, as a result, only 2 recipes are displayed, etc.

I did my research and found out that this is because the first line will be ignored and therefore not displayed.

Is there anyone who can suggest what kind of corrections to make to the cycle to fix this problem? My codes include display in a table, so I still can’t figure out what should be done, despite the other questions that have already been sent and answered.

Many thanks!

<?php

// 0: Instead of hard coding I shall declare the value first

$userid = $_SESSION['userid'];

// 1: Connect to forumdb database

$mysqli = new mysqli("localhost", "root", null, "recipedb") or exit("Error connecting to database");

// 2: Prepare the statement to select recipename,recipeid,,imagefile belonging to the $userid from recipe table

$stmt = $mysqli->prepare("Select recipename,recipeid,imagefile from recipe where userid=?");

// 3: Bind the values

$stmt->bind_param("s", $userid);

// 4: Execute the statement

$stmt->execute();

// TODO 5: bind results into $recipename,$recipeid and $imagefile

$stmt->bind_result($recipename, $recipeid, $imagefile);

if ($stmt->fetch() == null) {
    echo "You did not have any recipes yet.<br />";
}
else {
    echo "<table style=width:100% >";
    echo "<tr><td><b>Recipe</b></td><td><b>Actions</b></td></tr>";

    // Use while loop to fetch messages and put in a <table>
    // if

    while ($stmt->fetch()) {
        echo "<tr>";

        // 6: In 1st <td>, display recipename,recipeid,imagefile

        echo "<td><b>$recipename</b><br /><b>Recipe ID:</b>$recipeid<br /> <img src='images/$imagefile'    height='125' width='125'              >   </td>";

        // 7: In 2nd <td>, display View hyperlink
        // The View hyperlink links to recipedetails.php
        // The delete hyperlink links to deleterecipes.php

        echo "<td> <a href='recipedetails.php?recipeid=$recipeid'>View</a>&nbsp";
        echo "<a href='deleteconfirmation.php?recipeid=$recipeid'>Delete</a>&nbsp";
        echo "</tr>";
    }

    echo "</table>";
}

// 8: close the statement

$stmt->close();

// 9: close $mysqli

$mysqli->close();
?>
+4
source share
2 answers

As you say when you do this:

if($stmt->fetch()==null)

the code retrieves the first line. If it does not exist, the condition is triggered. Otherwise, this continues, and when you start fetching the rows “for real,” the first has already been extracted.

Instead, you can check the number of rows returned:

$stmt->store_result();
if ($stmt->num_rows == 0) {
    echo "You did not have any recipes yet.<br>";
}
+3
source

... . , 2 , ( while). . , while, "do while" , .

0

All Articles