Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有嵌入html抛出错误的php foreach_Php_Html_Mysql_Pdo - Fatal编程技术网

带有嵌入html抛出错误的php foreach

带有嵌入html抛出错误的php foreach,php,html,mysql,pdo,Php,Html,Mysql,Pdo,我试图使用PDO从mysql获取数据,并将其输出到一个html表中,如下所示 <div class="data"> <table class="table table-striped"> <?php if($_POST['submit'] && $_POST['search_for'] == "keyword") { $stmt = $db->prepare("SELECT * FR

我试图使用PDO从mysql获取数据,并将其输出到一个html表中,如下所示

  <div class="data">
    <table class="table table-striped">
      <?php 
        if($_POST['submit'] && $_POST['search_for'] == "keyword") {
          $stmt = $db->prepare("SELECT * FROM my_tbl WHERE log like '%?%' ORDER BY submission_timestamp DESC");
          foreach($stmt->execute($_POST['search_for']) as $row):
        } elseif($_POST['submit'] && $_POST['search_for'] == "address") {
          $stmt = $db->prepare("SELECT * FROM my_tbl WHERE address like '?' ORDER BY submission_timestamp DESC");
          foreach($stmt->execute($_POST['search_for']) as $row):
        } else {
          foreach($db->query('SELECT * FROM my_tbl ORDER BY submission_timestamp DESC') as $row): 
        }
      ?>
        <tr>
          <td width="10%"><?php echo $row['address'] ?></td>
          <td><?php echo $row['log'] ?></td>
          <td width="10%"><?php echo $row['submission_timestamp'] ?></td>
        </tr>
      <?php endforeach; ?>
    </table>
  </div>


在使用另一个
foreach
语句之前,应先结束第一个语句

所以看起来像这样

<div class="data">
<table class="table table-striped">
  <?php 
    if($_POST['submit'] && $_POST['search_for'] == "keyword") {
      $stmt = $db->prepare("SELECT * FROM my_tbl WHERE log like '%?%' ORDER BY submission_timestamp DESC");
      foreach($stmt->execute($_POST['search_for']) as $row):
      endforeach;
    } elseif($_POST['submit'] && $_POST['search_for'] == "address") {
      $stmt = $db->prepare("SELECT * FROM my_tbl WHERE address like '?' ORDER BY submission_timestamp DESC");
      foreach($stmt->execute($_POST['search_for']) as $row):
      //No code here?
      endforeach;
    } else {
      foreach($db->query('SELECT * FROM my_tbl ORDER BY submission_timestamp DESC') as $row): 
    }
  ?>
    <tr>
      <td width="10%"><?php echo $row['address'] ?></td>
      <td><?php echo $row['log'] ?></td>
      <td width="10%"><?php echo $row['submission_timestamp'] ?></td>
    </tr>
  <?php endforeach; ?>
</table>



我还很好奇这里使用的是什么数据库抽象层,因为我所熟悉的任何数据库抽象层都不会在
查询中返回结果集,而是返回资源ID,您仍然需要使用该ID在指示您希望返回的结果集类型(即关联数组、对象、,等等……我认为他实际上想做的是使用底部代码作为从顶部开始的
foreach
的主体。@ohgod为什么我认为它是PDO。它的语句对象实现了
Iterable
@Barmar是的,这就是我要做的。我希望我可以开始一个基于条件语句的foreach,然后其余的都会失败,然后我可以输出我的表数据并在完成后结束它。谢谢。我没想过要把foreach放在那样的结尾。
<div class="data">
<table class="table table-striped">
  <?php 
    if($_POST['submit'] && $_POST['search_for'] == "keyword") {
      $stmt = $db->prepare("SELECT * FROM my_tbl WHERE log like '%?%' ORDER BY submission_timestamp DESC");
      foreach($stmt->execute($_POST['search_for']) as $row):
      endforeach;
    } elseif($_POST['submit'] && $_POST['search_for'] == "address") {
      $stmt = $db->prepare("SELECT * FROM my_tbl WHERE address like '?' ORDER BY submission_timestamp DESC");
      foreach($stmt->execute($_POST['search_for']) as $row):
      //No code here?
      endforeach;
    } else {
      foreach($db->query('SELECT * FROM my_tbl ORDER BY submission_timestamp DESC') as $row): 
    }
  ?>
    <tr>
      <td width="10%"><?php echo $row['address'] ?></td>
      <td><?php echo $row['log'] ?></td>
      <td width="10%"><?php echo $row['submission_timestamp'] ?></td>
    </tr>
  <?php endforeach; ?>
</table>
<div class="data">
    <table class="table table-striped">
      <?php 
        if($_POST['submit'] && $_POST['search_for'] == "keyword") {
          $stmt = $db->prepare("SELECT * FROM my_tbl WHERE log like '%?%' ORDER BY submission_timestamp DESC");
          $rows = $stmt->execute($_POST['search_for'];
        } elseif($_POST['submit'] && $_POST['search_for'] == "address") {
          $stmt = $db->prepare("SELECT * FROM my_tbl WHERE address like '?' ORDER BY submission_timestamp DESC");
          $rows = $stmt->execute($_POST['search_for'];
        } else {
          $rows = $db->query('SELECT * FROM my_tbl ORDER BY submission_timestamp DESC') ;             
        }
        foreach($rows as $row): 
      ?>
        <tr>
          <td width="10%"><?php echo $row['address'] ?></td>
          <td><?php echo $row['log'] ?></td>
          <td width="10%"><?php echo $row['submission_timestamp'] ?></td>
        </tr>
      <?php endforeach; ?>
    </table>
  </div>