php - mysql_fetch_assoc 返回重复的行...

标签 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>

提前致谢。

-伦茨

最佳答案

如果查询在 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'

关于php - mysql_fetch_assoc 返回重复的行...,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5198738/

相关文章:

MySQL 根据选定的列在组内重复行

php - jQuery - $.ajax 成功回调数据为空

mysql - 处理选择查询mysql上的空值

java - 将参数传递给 Jasper Reports 时出现的问题

java - 调用 line.isEmpty() 方法时出现重复的答案输出

r - 使用来自另一个数据框的引用清除重复项

PHP对GET参数的保护

php - 如何使用自定义 URL 方案 IOS Objective-c PHP

javascript - 评估 PHP 和 JavaScript 中的二进制运算

sql - 如何在分区的 MySQL 数据库中的字段上添加唯一索引?