[英]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.
每行都有一个称为MOrder的列,范围为1-可用模块数。
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? 但是,由于何种原因,该语句丢失了带有MOrder 1且始终以2开头的模块?
Why is this? 为什么是这样?
You are calling $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
您正在调用
$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
循环中将其覆盖:
while ($row = mysqli_fetch_array($result)) { // <-- overwritten here with item 2
//...
}
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. 再次,因此用第二行覆盖$ row变量。 Get rid of the 1st
$row = mysqli_fetch_array($result)
line. 摆脱第一
$row = mysqli_fetch_array($result)
行。
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>';
At the beginning, you fetch the first row. 首先,您获取第一行。
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
In while loop, fetch function returns second row. 在while循环中,提取函数返回第二行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.