下面的第一个代码是我的代码中的重复代码:
php $con=mysqli_connect("localhost","root","","task");
$results = mysqli_query($con, "SELECT * FROM note");
while ($row = mysqli_fetch_array($results)) {
我想删除它,同时仍然可以获取想要的数据。 我该怎么做?
<?php $con=mysqli_connect("localhost","root","","task");
$results = mysqli_query($con, "SELECT * FROM note");
while ($row = mysqli_fetch_array($results)) {
$id = $row['id'];
echo '<button class="call_modal" style="cursor:pointer; width: 150px; height: 60px;">' .$row['title']. '</button>';
echo "<a href='update.php?id=$id'>edit</a>";
echo ' ';
echo "<a href='delete.php?id=$id'>delete</a>";
}
?>
<?php $results = mysqli_query($con, "SELECT * FROM note"); ?>
<?php while ($row = mysqli_fetch_array($results)) { ?>
<div class="modal">
<div class="modal_close close"></div>
<div class="modal_main">
<div class="note">
<?php
echo '<br><br>';
echo '<div class="padding">'.$row['title'].'';
echo '<br><br><br><br>';
echo ''.$row['note'].'</div>';
echo '<br><br><br><br><br>';
echo '<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload File" name="submit">
</form>'
?>
<img src="x.png" class="close" style="line-height: 12px;
margin-top: 1px;
margin-right: 2px;
position:absolute;
top:0;
right:0;
width: 15px;
height:15px;">
</div>
</div>
<?php
}?>
检查是否正确:
<?php $con=mysqli_connect("localhost","root","","task");
$results = mysqli_query("SELECT * FROM note");
$rows = mysqli_fetch_all($results, MYSQLI_ASSOC);
foreach ($rows as $row) {
$id = $row['id'];
echo '<button class="call_modal" style="cursor:pointer; width: 150px; height: 60px;">' .$row['title']. '</button>';
echo "<a href='update.php?id=$id'>edit</a>";
echo ' ';
echo "<a href='delete.php?id=$id'>delete</a>";
}
?>
<?php foreach ($rows as $row) { ?>
<div class="modal">
<div class="modal_close close"></div>
<div class="modal_main">
<div class="note">
<?php
echo '<br><br>';
echo '<div class="padding">'.$row['title'].'';
echo '<br><br><br><br>';
echo ''.$row['note'].'</div>';
echo '<br><br><br><br><br>';
echo '<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload File" name="submit">
</form>'
?>
<img src="x.png" class="close" style="line-height: 12px;
margin-top: 1px;
margin-right: 2px;
position:absolute;
top:0;
right:0;
width: 15px;
height:15px;">
</div>
</div>
<?php
}?>
答案 0 :(得分:0)
如果您的目标是不两次执行同一查询,则必须重新使用结果。有几种方法可以做到这一点:
示例:
$results = mysqli_query("SELECT * FROM note");
while ($row = mysqli_fetch_array($results)) {
// do something
}
mysqli_data_seek($results, 0);
while ($row = mysqli_fetch_array($results)) {
// do something else
}
为什么这样做是必需的: mysqli_fetch_array
更新$results
中的内部指针,该指针告诉它下一行是什么。当第一个循环结束时,该内部指针将设置为最后一行的 beyond ,以指示没有更多行可用。当您的第二个循环开始时,该指针仍设置在那里,因此不会有任何结果可以循环通过。使用mysqli_data_seek
可以将指针手动设置回开头。
示例:
$results = mysqli_query("SELECT * FROM note");
$rows = mysqli_fetch_all($results, MYSQLI_ASSOC);
foreach ($rows as $row) {
// do something
}
foreach ($rows as $row) {
// do something else
}
使用mysqli_fetch_all
将检索所有行作为数组。然后,您可以根据需要在该数组上循环多次。我明确要求提供MYSQLI_ASSOC
结果,因为您使用的是$row['id']
和$row['note']
。 mysqli_fetch_all()
默认使用MYSQLI_NUM
结果,这将不允许您这样做。
AFAIK,这是最常见的方法,但是它有一个缺点:您正在将所有结果检索到内存中。如果您的查询有很多结果,或者您的应用程序已经在使用大量内存,则可能是一个问题。
由于您是
foreach
的新手,所以这里简要说明了它与其他循环方法的不同之处:foreach ($rows as $index => $row) { } // is the same as: for ($index = 0; $index < count($rows); $index++) { $row = $rows[$index]; } // or: $index = 0; while ($index < count($rows)) { $row = $rows[$index]; } // and if you're not interested in it, you don't need to ask for the $index: foreach ($rows as $row) { }
主要区别在于
foreach
在非数字数组上也很容易工作。例如:foreach ($row as $column => $value) { echo $column . ': ' . $value . '<br>'; // id: 123 // title: Note title // note: Lorem ipsum ... }