user3522892
user3522892

Reputation: 85

Retrieve all row but the first record is missing

$query = "SELECT * FROM abc";

    if ($result = $db->query($query)) {

      $row = $result->fetch_assoc();


        while ($row = $result->fetch_assoc()) {

            $data[] = array("a"=>$row["a"], 
                            "b"=>$row["b"],
                            "c"=>$row["c"],
                            "d"=>$row["d"]
                            ); 


        }

        $result->close();
    }

$db->close();

        <?php foreach ($data as $row) { ?>

                <form action="">
                    <div class="col-lg-3">
                        <input type="text" value="<?php echo $row['a'] ?>"/>
                    </div>
                    <div class="col-lg-3">
                        <input type="text" value="<?php echo $row['b'] ?>"/>
                    </div>
                    <div class="col-lg-3">
                        <input type="text" value="<?php echo $row['c'] ?>"/>
                    </div>
                    <div class="col-lg-3">
                        <input type="text" value="<?php echo $row['d'] ?>"/>
                    </div>
                </form>

              <?php } ?>

Above is the exact code that I used to retrieve all the row in a table. The strange part is that I have 5 row and it only show 4. The first row is missing. I wonder why.

Upvotes: 2

Views: 1454

Answers (1)

Jo&#235;l Salamin
Jo&#235;l Salamin

Reputation: 3576

In your code, you're calling $row = $result->fetch_assoc(); just before starting your while loop. This line is "consuming" your first raw and when you're entering in the loop, you move the cursor to the second row by calling this same method a second time.

The condition of the while loop is executed BEFORE the content, here you'll find more info about PHP While loop

To fix your code, remove this first line.

Upvotes: 4

Related Questions