doudun3040 2018-10-03 10:02
浏览 40
已采纳

这个while循环不起作用和更多时间,即使我给出条件

I used while loop to take data from the database and I give the condition, but the loop is repeated polytime even I am stopping from the browser and if I put break just it displays one record. if I remove the break it again more and more records.

<?php
$db=mysqli_connect("localhost","root","root","done");
$query="SELECT * FROM dones where id>3";
$result=mysqli_query($db,$query); 
$x=0;
while($row=mysqli_fetch_array($result))
{
 $x++;
  echo '<div class="accordion" id="accordionExample">';
  echo '<div class="card" style="margin-left:10px; margin-right:10px;">';
   echo '<div class="card-header" id="headingOne">';
     echo '<h5 class="mb-0">';
      echo  '<button class="btn btn-link" style="text-decoration:none; font- 
  family:Times New Roman, Times, serif; font-size:18px; " type="button" 
  data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" 
  aria-controls="collapseOne">';
      echo   $row['title'];

       echo '</button>';
      echo '</h5>';
    echo '</div>';

   echo '<div id="collapseOne" class="collapse show" aria- 
   labelledby="headingOne" data-parent="#accordionExample">';
   echo  '<div class="card-body">';



   echo '<form action="" method="POST" enctype="multipart/form-data" 
   style="height:430px;">';
   echo '<h5 style="text-align:left; margin-left:20px; margin-top:20px;"> 
   <small>Title</small></h5>';
   echo '<input style="width:290px; margin-left:158px; height:30px; margin- 
   top:-30px;" name="title" type="text" class="form-control" 
   placeholder="Title">';
   echo ' <br>';
   echo ' <div class="custom-file" style="height:10px; width:290px; margin- 
   right:1050px; top-padding:20px; float:right;">';
   echo ' <input type="file" name="uploadfile" class="custom-file-input" 
   id="validatedCustomFile">';
   echo ' <label class="custom-file-label" for="validatedCustomFile">Choose 
   file...</label>';
   echo ' </div>';
   echo ' <h5 style="text-align:left; margin-left:20px;"><small>File</small> 
   </h5>';
   echo ' <br>';
   echo ' <h5 style="text-align:left; margin-left:20px;"><small>Banner 
   Image</small></h5>';
   echo ' <div style="height:130px; width:290px; border-width:1px; border- 
   style:solid; float:right; margin-right:1050px; margin-top:-15px;">';

     $db=mysqli_connect("localhost","root","root","done");
     $sql="SELECT image FROM dones where status=1";
     $result=mysqli_query($db,$sql);
     $data = mysqli_fetch_array($result);
     echo '<img src= "images/'.$data['image'].'" style="height:130px; 
    width:290px;">';

     echo '</div>';
     echo '<br>';
     echo '<br>';
     echo '<br>';
     echo '<br>';
     echo '<br>';
     echo '<br>';  
     echo '<h5 style="text-align:left; margin-left:20px;"> 
    <small>Status</small></h5>';
     echo '<select name="status" id="" style="width:290px; float:right; 
     margin-right:1050px; margin-top:-25px;">';
     echo '<option value="1" name="enable">Enable</option>';
     echo '<option value="0" name="disable">Disable</option>';
     echo '</select>';
     echo '<br>';
     echo '<br>';
     echo '<button type="submit" name="tbsubmit"  style="margin-left:250px;" 
     class="btn btn-primary">Save Changes</button>';   
     echo '</div>';

     echo '</form>';
     echo '</div>';



    echo  '</div>';
   echo '</div>';
    echo '</div>';
   echo '</div>';

  }   

  ?>

just I want to repeat this loop, up to how many recordes I have in the database not more or less.

  • 写回答

1条回答 默认 最新

  • dongshao1021 2018-10-03 10:16
    关注

    You are re-using the first result set variable $result ...

    while($row=mysqli_fetch_array($result))
    

    inside the while loop...

     $db=mysqli_connect("localhost","root","root","done");
     $sql="SELECT image FROM dones where status=1";
     $result=mysqli_query($db,$sql);
     $data = mysqli_fetch_array($result);
    

    You can replace this with (also dropping the connection as you don't need to reconnect each time)...

     $sql="SELECT image FROM dones where status=1";
     $result1=mysqli_query($db,$sql);
     $data = mysqli_fetch_array($result1);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大