0

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 WHERE 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?

5 Answers 5

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

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

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'];
}
0
0

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?

0
0

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.

0

Try the following:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.