檢索所有行但第一條記錄丟失
2023-09-25
php問題
html5模板網
Retrieve all row but the first record is missing(檢索所有行但第一條記錄丟失)
本文介紹了檢索所有行但第一條記錄丟失的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
$query = "SELECT * FROM abc";如果 ($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 class="col-lg-3"><input type="text" value="<?php echo $row['b'] ?>"/>
<div class="col-lg-3"><input type="text" value="<?php echo $row['c'] ?>"/>
<div class="col-lg-3"><input type="text" value="<?php echo $row['d'] ?>"/>
</表單><?php } ?>
上面是我用來檢索表中所有行的確切代碼.奇怪的是我有 5 行,它只顯示 4.第一行丟失了.我想知道為什么.
解決方案
在你的代碼中,你在開始你的 $row = $result->fetch_assoc();
>while 循環.這條線是消耗"的您的第一個原始數據,當您進入循環時,您通過第二次調用相同的方法將光標移動到第二行.
while
循環的條件在內容之前執行,在這里您將找到有關 PHP While 循環
要修復您的代碼,請刪除第一行.
$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.
解決方案
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.
這篇關于檢索所有行但第一條記錄丟失的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!