fetchAll仅从两个连接的表中获取一行

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

我有两个表的联接。实际上,在我插入之前,代码和获取工作正常:

$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循环,所以我不明白为什么它只提取第一行。也许有人可以帮忙,谢谢!

php sql join pdo
1个回答
0
投票

您在这里的错误:

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

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

© www.soinside.com 2019 - 2024. All rights reserved.