开发者

Printing Duplicate Records In PHP / Mysql

I am trying to print the duplicate records of the table but only the single row is getting echoed.However in mysql this query results all the duplicate records. Here is the query:

$q = mysql_query("SELECT * FROM add WH开发者_如何转开发ERE cust_id = '144' GROUP BY cust_id");
$r = mysql_fetch_array($q);
$s = mysql_num_rows($q);

while($s !=0)
{
    echo $r;
    $s=$s-1;
}

Whats wrong with the code?


$q = mysql_query("SELECT * FROM add WHERE cust_id = '144' GROUP BY cust_id");

while($r = mysql_fetch_array($q))
{
    print_r($r);
}


You need to loop through the entire record set... you are only grabbing the first row:

$resultset = mysql_query("select * from add where cust_id = '144' group by cust_id");
while($row = mysql_fetch_assoc($resultset))
{
   echo $row['column_name'];
}


Your SQL query will in practice only ever return 0 or 1 rows, due to the GROUP BY clause. Are you absolutely sure that that's the query you were executing in mysql?


well, if you want to get duplicate values, then this query will serve you well:

T = the table f = the field to check for duplicates id = the rows id

select id,f from T group by f having count(f)= 2;

or >2 if you want every value in f that occurs in more than one row.

having is like where but evaluated after group by.


Try the following:

$q = mysql_query("SELECT * FROM add WHERE cust_id = '144'");
while($r = mysql_fetch_array($q))
{
    echo $r;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜