将数据库表中的数据显示到php文件中的html表中[重复]

问题描述 投票:-1回答:1

我正在尝试从数据库检索数据并将其显示在表中,但是我不知道如何在php中实现它。另外,我还需要一些帮助,以根据customer_id和date对显示的数据实施搜索功能。

<?php
include 'connect.php';
$query = ("SELECT * FROM 'transaction'");
$result = mysqli_query($conn, $query);
?>
<!DOCTYPE html>
<html lang="en">
<body>
 <form class="form-inline my-2 my-lg-0" action="search2.php" method="POST">
        <input class="form-control mr-sm-2" type="text" placeholder="Search" aria-label="Search">
        <button class="btn btn-outline-success my-2 my-sm-0" type="submit" name="search">Search</button>
      </form>
    </div>
  </nav>
<!-- Table -->
<h1>Sales</h1>
          <div class="table-responsive">
            <table class="table table-striped table-sm">
              <thead>
                <tr>
                  <th>Customer ID</th>
                  <th>Date</th>
                  <th>Outlet</th>
                  <th>Employee ID</th>
                  <th>Product ID</th>
                  <th>Quantity</th>

                </tr>
              </thead>
              <tbody>
                <?php while ($row = mysqli_fetch_row($result)) :?>
    <tr>
      <th scope="row"><?php echo $i; ?></th>
        <td><?php echo $row['customer_id']; ?></td>
        <td><?php echo $row['date']; ?></td>
        <td><?php echo $row['outlet_id']; ?></td>
        <td><?php echo $row['emp_id']; ?></td>
        <td><?php echo $row['Prod_id']; ?></td>
        <td><?php echo $row['quantity']; ?></td>
    </tr>
<?php
if (!$result) {
  die ("Database access failed: " .mysql_error());
}
endwhile; ?>
              </tbody>
            </table>
          </div>

</body>
</html>

这是DB连接的单独文件。

<?php
$ser = "localhost";
$user = "root";
$pass = "";
$db = "music_store_db";

// Create connection
$conn = new mysqli($ser, $user, $pass, $db);

// Check connection
if (mysqli_connect_error()){
  die("Connection Failed: ".mysqli_connect_error());
}
// echo "Connection Success";
 ?>

单击此页面的链接时,它应该显示来自数据库的销售交易。

我得到的输出是:

警告:mysqli_fetch_row()期望参数1为mysqli_result,给定布尔值

php html mysql
1个回答
-1
投票

请使用由mysqli_fetch_row插入的mysqli_fetch_assoc并从查询中的表名之前和之后删除

<?php
include 'connect.php';
$query = ("SELECT * FROM transaction");
$result = mysqli_query($conn, $query);
?>
<!DOCTYPE html>
<html lang="en">
<body>
 <form class="form-inline my-2 my-lg-0" action="search2.php" method="POST">
        <input class="form-control mr-sm-2" type="text" placeholder="Search" aria-label="Search">
        <button class="btn btn-outline-success my-2 my-sm-0" type="submit" name="search">Search</button>
      </form>
    </div>
  </nav>
<!-- Table -->
<h1>Sales</h1>
          <div class="table-responsive">
            <table class="table table-striped table-sm">
              <thead>
                <tr>
                  <th>Customer ID</th>
                  <th>Date</th>
                  <th>Outlet</th>
                  <th>Employee ID</th>
                  <th>Product ID</th>
                  <th>Quantity</th>

                </tr>
              </thead>
              <tbody>
                <?php while ($row = mysqli_fetch_assoc($result)) :?>
    <tr>
      <th scope="row"><?php echo $i; ?></th>
        <td><?php echo $row['customer_id']; ?></td>
        <td><?php echo $row['date']; ?></td>
        <td><?php echo $row['outlet_id']; ?></td>
        <td><?php echo $row['emp_id']; ?></td>
        <td><?php echo $row['Prod_id']; ?></td>
        <td><?php echo $row['quantity']; ?></td>
    </tr>
<?php
if (!$result) {
  die ("Database access failed: " .mysql_error());
}
endwhile; ?>
              </tbody>
            </table>
          </div>

</body>
</html>
© www.soinside.com 2019 - 2024. All rights reserved.