当我使用下面的代码时,我的mysql代码返回重复值:
首先,这是我的代码:
$customer = htmlspecialchars($_GET['customer']);
$STH = $DBH->query( "SELECT DISTINCT customer FROM table WHERE customer LIKE
'$customer%' LIMIT 4" );
$STH->setFetchMode(PDO::FETCH_ASSOC);
while( $row = $STH->fetch() ){
$customer_name = $row['customer'];
$list .= "<p>" .$customer_name. "</p>";
echo $list;
}
我的SELECT语句有问题吗?
我读到,当空格等数据条目存在细微差别时,可以返回重复值。我检查并重新检查了条目,它们在整个过程中完全相同。
答案 0 :(得分:2)
试试这个。
$customer = htmlspecialchars($_GET['customer']);
$STH = $DBH->query( "SELECT DISTINCT customer FROM table WHERE customer LIKE
'$customer%' LIMIT 4" );
$STH->setFetchMode(PDO::FETCH_ASSOC);
while( $row = $STH->fetch() ){
$customer_name = $row['customer'];
$list .= "<p>" .$customer_name. "</p>";
}
echo $list;
您是每个周期的回音列表。我认为情况就是这样。
答案 1 :(得分:1)
每次迭代都会附加$list
,然后每次都回显整个事件。因此,如果查询返回Person1,Person2和Person3,那么这将是您的输出:
Person1 # End of loop iteration 1
Person1
Person2 # End of loop iteration 2
Person1
Person2
Person3 # End of loop iteration 3