【问题标题】:Page keeps loading don't show result while loop php页面不断加载不显示结果而循环php
【发布时间】:2020-06-13 14:30:08
【问题描述】:

我有一个从数据库中查找结果的函数。我正在使用while loopmysqli_fetch_assoc() 从数据库中获取结果。但是页面一直在加载并且没有显示任何错误。

循环

  <?php while ($job = mysqli_fetch_assoc(find_all_jobs())) { ?>
  <tr class="custom-table-body-titles">
    <td><?php echo $job['updated-at']; ?></td>
    <td>
      <a href="#" class="text-dark"><?php echo $job['title']; ?></a>
    </td>                
    <td>0/<?php echo $job['required_freelancers']; ?></td>
    <td><?php echo $job['delivery_time']; ?> Days</td>
    <td>$<?php echo $job['budget']; ?></td>
    <td>
      <a href="job_details.html" class="btn btn-sm btn-primary">Apply</a>
    </td>
  </tr>
  <?php } ?>

功能

function find_all_jobs() {
  global $connection;
  $sql = "SELECT * FROM jobs ORDER BY job_id DESC LIMIT 10";
  $stmt = mysqli_stmt_init($connection);
  mysqli_stmt_prepare($stmt, $sql);
  $result = mysqli_stmt_execute($stmt);
  if(!$result) {
    die("Database query failed." . mysqli_stmt_error($stmt));
  }
  return mysqli_stmt_get_result($stmt);
  mysqli_stmt_close($stmt);  
}

【问题讨论】:

    标签: php mysql while-loop


    【解决方案1】:

    find_all_jobs() 的结果分配给一个变量并运行你的循环:

    <?php 
    $jobs = find_all_jobs();
    while ($job = mysqli_fetch_assoc($jobs)) { ?>
    

    因为当前您的 find_all_jobs() 在每次 while 迭代时执行。因此会导致无限循环

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-09
      • 2019-03-18
      • 1970-01-01
      相关资源
      最近更新 更多