mysqli忽略表中的第一行 (mysqli ignoring the first row in a table)


問題描述

mysqli忽略表中的第一行 (mysqli ignoring the first row in a table)

Here is the code I'm using to pull the data from the table:

require_once 'connect.php';
$sql = "SELECT * FROM `db‑news`";
$result = $mysqli‑>query($sql);
$row = mysqli_fetch_assoc($result);
while ($row = $result‑>fetch_assoc()) {
    printf ($row['pagename'].' ‑ To edit this page <a href="editnews.php?id='.$row['id'].'">click here</a><br>');
}

Always the first row is ignored. I'm not calling mysqli_fetch_assoc twice as with some other examples on SO. I've tried changing echo to printf in the while loop and still the first row is ignored in the DB.

I'm at a loss as to what I should try next?

‑‑‑‑‑

參考解法

方法 1:

mysqli not ignoring it but actually you are fetching first row before while loop

$row = mysqli_fetch_assoc($result); //remove this line
while ($row = $result‑>fetch_assoc()) {
   ....
}

方法 2:

The problem is the first row of the following excerpt:

$row = mysqli_fetch_assoc($result);
while ($row = $result‑>fetch_assoc()) {

The mysqli_fetch_assoc already gets the first row (and thus in the while loop you are already 1 step further). You should either put that line instead of the $reslut‑>fetch_assoc part into the while statement or delete it. That should solve the problem.

方法 3:

mysqli_fetch_assoc() is the same as $result‑>fetch_assoc() and whenever you call this function it will advance an internal pointer to the next row. You are calling this function once just before the loop which means that you are reading the first row and ignoring the result. Remove that line.

If you need to fetch the first line and then still loop through the whole result use foreach instead of while. For example:

require_once 'connect.php';
$sql = "SELECT * FROM `db‑news`";
$result = $mysqli‑>query($sql);
foreach($result as $row) {
    printf($row['pagename'].' ‑ To edit this page <a href="editnews.php?id='.$row['id'].'">click here</a><br>');
}

(by williamsongibsonGBDThomasDharman)

參考文件

  1. mysqli ignoring the first row in a table (CC BY‑SA 3.0/4.0)

#mysqli #PHP






相關問題

如何第一次創建 mysqli 數據庫? (how can I creates a mysqli database for first the first time?)

使用 jquery autosuggest 從多個表中選擇 (select from multiple tables with jquery autosuggest)

無法連接 - 數據庫或編碼錯誤 (Could Not Connect - Database or Coding Error)

mysqli忽略表中的第一行 (mysqli ignoring the first row in a table)

從數據庫導入的鏈接以 UTF8 截斷 (imported link from database cut off at UTF8)

試圖獲取非對象的屬性 (Trying to get property of non-object)

獲取 php 和 mysqli 中插入數據的 ID (Get Id of inserted data in php and mysqli)

WAMP 和 mysqli::real_connect(): (HY000/2002)? (WAMP and mysqli::real_connect(): (HY000/2002)?)

如何將 MySQLi 準備好的語句的結果放入關聯數組中? (How can I put the results of a MySQLi prepared statement into an associative array?)

測試選擇查詢是否找到結果 (Testing if a Select Query found results)

為什麼我的正確連接嘗試在 apache2 下失敗但在 cli 下失敗? (Why is my correct connection attempt failing under apache2 but not under cli?)

在帶有數組的 IN 語句之後,在 WHERE 語句中使用更多佔位符 (Use more placeholders in WHERE statement after IN statement with arrays)







留言討論