-1

I am trying to get data from multiple tables and I got them while fetching data I am getting same data multiple time

How to stop echo data multiple times

<?php
$stmt = $DB_con->prepare('SELECT t1.id,t2.id from t1,t2ORDER BY t1.id DESC ');
$stmt->execute();   
if($stmt->rowCount() > 0){
    while($row=$stmt->fetch(PDO::FETCH_ASSOC)){
        extract($row);
}
?>

This I am getting data but multiple times like t1 have 10 ids t2 have 20 ids so I got 30 IDs on the display

GYaN
  • 2,327
  • 4
  • 19
  • 39
user2226832
  • 3
  • 1
  • 7

1 Answers1

0

for avoid duplicated rows you could use distinct

  $DB_con->prepare('SELECT DISTINCT  t1.id,t2.id from t1,t2 ORDER BY t1.id DESC ');

but in your FROM clause you are using a cross join (a cartesian product between each row of a table multiplied by each row for other table)

could be you need a join alia a condition between tables column thate relate the tow table ed.

  $DB_con->prepare('SELECT DISTINCT  t1.id,t2.id 
  from t1
  INNER JOIN t2 ON t1.id = t2.id ORDER BY t1.id DESC ');

the join based on t1.id = t2.id is just a sample you should check for the evnetual real joining condition

ScaisEdge
  • 131,976
  • 10
  • 91
  • 107