kush
kush

Reputation: 93

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

Upvotes: 0

Views: 838

Answers (5)

user838417
user838417

Reputation: 61

Try the following:

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

Upvotes: 0

Raffael
Raffael

Reputation: 20045

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.

Upvotes: 0

Dejan Marjanović
Dejan Marjanović

Reputation: 19380

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

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

Upvotes: 1

GordonM
GordonM

Reputation: 31730

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?

Upvotes: 0

prodigitalson
prodigitalson

Reputation: 60413

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

Upvotes: 0

Related Questions