fetchall只从两个联接表中获取一行

jfgube3f  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(584)

我有两张table。实际上,在我插入之前,代码和获取工作正常:

$id=isset($_GET['id']) ? $_GET['id'] : die('ERROR: Record ID not found.');

WHERE id = ? LIMIT 0,1";

但现在它只获取第一行,即使有两行:

<head>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<?php

  $id=isset($_GET['id']) ? $_GET['id'] : die('ERROR: Record ID not found.');

  try {
  require "config.php";
  require "common.php";

  $connection = DbConnect();

  $sql = "SELECT * FROM product
LEFT JOIN brand
ON product.id = brand.product_id
WHERE id = ? LIMIT 0,1";

  $statement = $connection->prepare($sql);
  $statement->bindParam(1, $id);
  $statement->execute();

  $result = $statement->fetchAll(PDO::FETCH_ASSOC);

} catch(PDOException $error) {
  echo $sql . "<br>" . $error->getMessage();
}
?>
<?php require "public/templates/header.php"; ?>

<h2>Update </h2>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"] . "?id={$id}");?>" method="post">
<table>
  <thead>
    <tr>
      <th>id</th>
      <th>product name</th>
      <th>date</th>
      <th>brand name</th>
      <th>brand location</th>
    </tr>
  </thead>
    <tbody>

    <?php foreach ($result as $row) : ?>
      <tr>
        <td><?php echo escape($row["id"]); ?></td>
        <td><?php echo escape($row["product_name"]); ?></td>
        <td><?php echo escape($row["date"]); ?></td>
        <td><?php echo escape($row["brand_name"]); ?></td>
        <td><?php echo escape($row["brand_location"]); ?></td>
        <td><a href="entry_update.php?id=<?php echo escape($row["id"]); ?>">Select</a></td>
      </tr>
    <?php endforeach; ?>
    </tbody>
</table>
</form>
<a href="index.php">Back to home</a>

<?php require "public/templates/footer.php"; ?>

我使用了fetchall和foreach循环,所以我不明白为什么它只获取第一行。也许有人能帮忙,谢谢!

8tntrjer

8tntrjer1#

你的错误在这里:

$sql = "SELECT * FROM product LEFT JOIN brand ON product.id = brand.product_id WHERE id = ? LIMIT 0,1";

你使用限制上下文,它返回一行。尝试将限制0,1更改为限制10。

相关问题