There are two tables, comments and users. They are related. It is necessary to obtain data from both tables in a single query. I make such a request

$Result = mysqli_query($CONNECT, 'SELECT comments.id, comments.added, comments.date, comments.text, users.id, users.fname, users.lname, users.avatar, users.login FROM comments, users ORDER BY comments.id DESC'); 

And bring:

 while ($Row = mysqli_fetch_assoc($Result)) { echo ' <h6 class="comment__name"> <a href="/users/'.$User['login'].'">'.$Row['fname'].' '.$Row['added'].' '.$Row['lname'].'</a></h6> <span>'.$Row['date'].'</span> <div class="comment__content">'.$Row['text'].'</div> ';} 

The problem is this: the same string while displays 2 times.

Thanks Mike for the tip, the solution:

 $Result = mysqli_query($CONNECT, ('SELECT comments.id, comments.added, comments.date, comments.text, users.id, users.fname, users.lname, users.avatar, users.login FROM comments, users WHERE comments.added = users.login ORDER BY comments.id DESC')); 
  • one
    It is strange that only two. It should display all records from the first table in the number of records from the second table, because you did not specify in the where clause the condition which rows to link with which - Mike
  • If the tables are not related, which user next to which comment should you show? - Mike
  • That user who added this comment - Puvvl
  • And you say that the tables are not connected ... They are just related by this feature. How is the field with the id of the user who left the comment called in the comments table? - Mike
  • thanks)) wrote a solution above. - Puvvl

2 answers 2

If the tables are related by id use, for example, such a query:

 SELECT c.id, c.added, c.date, c.text, u.fname, u.lname, u.avatar, u.login FROM comments c, users u WHERE c.id = u.id ORDER BY c.id DESC 

or use join .

    If the query itself returns 2 rows each, add DISTINCT after SELECT

     SELECT DISTINCT comments.id, comments.added, comments.date, comments.text, users.id, users.fname, users.lname, users.avatar, users.login FROM comments, users ORDER BY comments.id DESC 
    • 3
      Yeah, ran away. he wrote tables without the join condition, there will be many entries and all different comment 1 user 1, then comment 1 user 2, etc. and most importantly they will be even for one field, but different and distinct will not help - Mike
    • What is the difference, if now displays correctly, only zadvoenno. Another thing is that I looked through that some of the fields displayed in the select does not participate in the output, and the distinction on them may not work. But in this case, they can simply be removed, because they are not displayed. Or add "subtleties" of the task. But for the current description of distinc fit. - T.Zagidullin
    • This is doubled because there are only 2 records in one of the tables - Mike
    • Let 2, the dist fix it fix. I do not say that the decision with joyno is not correct, it is more correct. But I think that addressing such a question, the author would be glad to the simplest solution. distinctive simpler than join for some reason :) - T.Zagidullin
    • Yes, the author simply does not understand that his values ​​are not doubled, and that in the lines only the last fields are different, and only the beginning is doubled ... and distinct will not fix it. and the join in the question is already present, only in the form of an equivalent comma operator - Mike