mysql_fetch_assoc返回重复的行...

时间:2011-03-04 20:11:04

标签: php mysql duplicates rows

我的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

2 个答案:

答案 0 :(得分:0)

如果查询在phpMyAdmin中运行相同,则有两种选择。首先,您可以清理数据。如果可能的话,那就是最好的方向。更好的数据可以实现更好的应用如果您无法为数据完整性做太多工作,那么您需要对其进行说明。最简单的方法是为查询添加一个独特的....

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'

答案 1 :(得分:0)

你的条款

ON c.customer_date = s.date_in

应该是一个唯一的密钥

ON c.room_number = s.room_number