为foreach()提供的PHP数据库无效参数 [英] PHP Database Invalid argument supplied for foreach()

查看:88
本文介绍了为foreach()提供的PHP数据库无效参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是PHP的新手,因为我两天前才开始学习! 我试图在index.html文件中正确显示我的第二张表,但是出现以下错误:为foreach()提供的参数无效看看

Hi I'm very new to PHP because I just started learning this 2 days ago! I'm trying to display my second table properly in my index.html file but I get this error: Invalid argument supplied for foreach() Take a look

任何帮助将不胜感激

班级

public class Category {
    public int Id{get;set;}
    public string Name {get;set;}
    }
public class Product{
    public int Id{get;set;}
    public int CategoryId{get;set;}
    public String Brand {get;set;}
    public String Name {get;set;}
    public decimal Price{get;set;
}

index.php

index.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <link   href="css/bootstrap.min.css" rel="stylesheet">
    <script src="js/bootstrap.min.js"></script>
</head>

<body>
    <div class="container">
            <div class="row">
                <h3>PHP CRUD Grid</h3>
            </div>
            <div class="row">
                <p>
                    <a href="create.php" class="btn btn-success">Create</a>
                </p>

                <table class="table table-striped table-bordered">
                      <thead>
                        <tr>
                          <th>Id</th>
                          <th>CategoryId</th>
                          <th>Brand</th>
                          <th>Name</th>
                      <th>Barcode</th>
                      <th>Price</th>
                        </tr>
                      </thead>
                      <tbody>
                      <?php
                       include_once 'database.php';
                       $pdo = Database::connect();
                       $sql = 'SELECT * FROM product ORDER BY id DESC';
                       foreach ($pdo->query($sql) as $row) {
                                echo '<tr>';
                                echo '<td>'. $row['id'] . '</td>';
                                echo '<td>'. $row['category_id'] . '</td>';
                                echo '<td>'. $row['brand'] . '</td>';
                  echo '<td>'. $row['name'] . '</td>';
                  echo '<td>'. $row['barcode'] . '</td>';
                  echo '<td>'. $row['price'] . '</td>';
                                echo '<td width=250>';
                                echo '<a class="btn" href="read.php?id='.$row['id'].'">Read</a>';
                                echo '&nbsp;';
                                echo '<a class="btn btn-success" href="update.php?id='.$row['id'].'">Update</a>';
                                echo '&nbsp;';
                                echo '<a class="btn btn-danger" href="delete.php?id='.$row['id'].'">Delete</a>';
                                echo '</td>';
                                echo '</tr>';
                       }

                      ?>
                      </tbody>
                </table>

              <div class="container">
              <div class="row">
              <h3>PHP CRUD Grid</h3>
              </div>
              <div class="row">
              <p>
              <a href="createCategory.php" class="btn btn-success">Create</a>
              </p>
              <table class="table table-striped table-bordered">
              <thead>
              <tr>
                <th>CategoryId</th>
                <th>Category Name</th>
              </tr>
            </thead>

            include_once 'database.php';
            <?php
            $sql2 = 'SELECT * FROM category ORDER BY id DESC';
            foreach ($pdo->query($sql2) as $row) {
                 echo '<tr>';
                 echo '<td>'. $row['id'] . '</td>';
                 echo '<td>'. $row['category_name'] . '</td>';
                 echo '<td width=250>';
                 echo '<a class="btn" href="readCategory.php?id='.$row['id'].'">Read</a>';
                 echo '&nbsp;';
                 echo '<a class="btn btn-success" href="updateCategory.php?id='.$row['id'].'">Update</a>';
                 echo '&nbsp;';
                 echo '<a class="btn btn-danger" href="deleteCategory.php?id='.$row['id'].'">Delete</a>';
                 echo '</td>';
                 echo '</tr>';
             }
             Database::disconnect();
            ?>
              </tbody>
            </table>
        </div>
    </div>


  </body>
</html>

推荐答案

删除第二个:

include 'database.php';

或使用:

include_once 'database.php'; 

防止双重加载数据库文件.

to prevent double loading of your database file.

您可能要考虑不要断开连接并再次连接

You might want to consider not disconnecting and connecting again

这篇关于为foreach()提供的PHP数据库无效参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆