user6618037
user6618037

Reputation:

Missing the first row of a SELECT statment

I have a database table called 'Modules' and I am trying to select all of the rows from that table and display them in a table. Each row has a column called MOrder which ranges from 1 - how ever many modules are available.

Here is my code:

    $sql_query = "SELECT * FROM Modules WHERE CourseID = ". $CourseID ." ORDER BY MOrder ASC";
    $result = mysqli_query($dbconfig, $sql_query);
    $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
    $count = mysqli_num_rows($result);

    echo '<div class="row">';
        echo '<div class="col-md-12">';
            echo '<table class="table">';
            if($count > 0) {
                $_SESSION['countMOrder'] = $count;

                echo '<tr>';
                    echo '<th>Module Title</th> ';
                    echo '<th></th>';
                echo '</tr>';
                while ($row = mysqli_fetch_array($result)) {
                    echo '<tr>';
                        echo '<td>'. $row['Title'] .'</td> ';
                        echo '<td><a href="user-view-module-revised.php?CourseID='. $row['CourseID'] .'&ModuleID='. $row['ID'] .'&MOrder='. $row['MOrder'] .'" title="Take Module" class="btn btn-success" id="'. $row['MOrder'] .'">Take Module</a></td>';
                    echo '</tr>';
                }
            }
            echo '</table>';
        echo '</div>';
    echo '</div>';
?>

However, for whatever reason the statement is missing out the module with MOrder 1 and always starting with 2?

Why is this?

Upvotes: 1

Views: 395

Answers (4)

selami
selami

Reputation: 2498

At the beginning, you fetch the first row.

$row = mysqli_fetch_array($result, MYSQLI_ASSOC);

In while loop, fetch function returns second row.

Upvotes: 0

Jyothi Babu Araja
Jyothi Babu Araja

Reputation: 10282

Try this code.

$sql_query = "SELECT * FROM Modules WHERE CourseID = ". $CourseID ." ORDER BY MOrder ASC";
    $result = mysqli_query($dbconfig, $sql_query);
    $count = mysqli_num_rows($result);

    echo '<div class="row">';
        echo '<div class="col-md-12">';
            echo '<table class="table">';
            if($count > 0) {
                $_SESSION['countMOrder'] = $count;

                echo '<tr>';
                    echo '<th>Module Title</th> ';
                    echo '<th></th>';
                echo '</tr>';
                while ($row = mysqli_fetch_array($result)) { //for every fetch we'll get one row.
                    echo '<tr>';
                        echo '<td>'. $row['Title'] .'</td> ';
                        echo '<td><a href="user-view-module-revised.php?CourseID='. $row['CourseID'] .'&ModuleID='. $row['ID'] .'&MOrder='. $row['MOrder'] .'" title="Take Module" class="btn btn-success" id="'. $row['MOrder'] .'">Take Module</a></td>';
                    echo '</tr>';
                }
            }
            echo '</table>';
        echo '</div>';
    echo '</div>';

Upvotes: 0

Shadow
Shadow

Reputation: 34231

Because in the 3rd line of your code you call

$row = mysqli_fetch_array($result, MYSQLI_ASSOC);

once, and then in the loop you start calling the

$row = mysqli_fetch_array($result)

again, thus overwriting the $row variable with the 2nd row. Get rid of the 1st $row = mysqli_fetch_array($result) line.

Upvotes: 0

msbit
msbit

Reputation: 4320

You are calling $row = mysqli_fetch_array($result, MYSQLI_ASSOC); in the third line of your pasted code, which is pulling the first array from the results.

This is then being overwritten in your while loop:

while ($row = mysqli_fetch_array($result)) { // <-- overwritten here with item 2
    //...
}

Upvotes: 1

Related Questions