【问题标题】:mysql_fetch_assoc returns duplicated rows...mysql_fetch_assoc 返回重复的行...
【发布时间】:2011-07-09 02:19:32
【问题描述】:

我的 fetch_assoc 返回重复的行。它似乎在自我繁殖。我的表中有 4 个输入,它返回 16。

这是我的代码....请帮助我。我想我的循环错了。

<?php
$tryshow =" SELECT c.customer_date, c.lastname, c.firstname,
   s.room_number, s.date_in, s.date_out
FROM customers c
    INNER JOIN services s
        ON c.customer_date = s.date_in
WHERE c.customer_date = '$customer_date' ";

$result = @mysql_query($tryshow,$conn)
            or die(mysql_error());    

if (mysql_num_rows($result) == 0) {
    echo "No rows found, nothing to print...";
}
?>
<form> 
<table width="700" border="0">
    <tr>
      <td width="100">Customer Date:</td>
      <td width="100">Last Name</td>
      <td width="100">First Name</td>
      <td width="100">Room Number</td>
      <td width="100">Date In</td>
      <td width="100">Date Out</td>
    </tr>
<?php while($row=mysql_fetch_assoc($result)){ ?>
    <tr>
      <td><?php echo $row['customer_date']; ?></td>
      <td><?php echo $row['lastname']; ?></td>
      <td><?php echo $row['firstname']; ?></td>
      <td><?php echo $row['room_number']; ?></td>
      <td><?php echo $row['date_in']; ?></td>
      <td><?php echo $row['date_out']; ?></td>
    </tr>
<?php  }?>
</table>

提前致谢。

-renz

【问题讨论】:

  • 很可能是您的数据。您可能为相同的客户提供具有相同“日期”的服务。不过只是猜测。代码本身看起来不错。在数据库上运行查询本身,看看你得到了什么
  • @cfreak 你的权利,我在 phpmyadmin 上运行它,它是一样的。你觉得我应该怎么做?
  • 修复数据完整性逻辑

标签: php mysql duplicates rows


【解决方案1】:

如果查询在 phpMyAdmin 中运行相同,您有两个选择。首先,您可以清理数据。如果这是可能的,这是最好的方向。更好的数据使更好的应用成为可能。如果您不能为数据完整性做太多事情,那么您需要考虑它。最简单的方法是在您的查询中添加一个 distinct....

SELECT distinct c.customer_date, c.lastname, c.firstname,
   s.room_number, s.date_in, s.date_out
FROM customers c
    INNER JOIN services s
        ON c.customer_date = s.date_in
WHERE c.customer_date = '$customer_date'

【讨论】:

  • 感谢您的回复。我已经尝试过不同但仍然相同的输出。
  • 你能发布实际的输出吗?
【解决方案2】:

你的 on 子句

ON c.customer_date = s.date_in

应该在一个唯一的键上

ON c.room_number = s.room_number

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-13
    • 2011-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-08
    • 2014-03-19
    相关资源
    最近更新 更多