I want to return Title only once and then a list of all users with correct n_id.
I have such table:
| N_ID | USER | TITLE |
|------|-------|--------|
| 1 | User1 | Title1 |
| 1 | User2 | Title1 |
| 1 | User3 | Title1 |
| 2 | User3 | Title5 |
My code so far:
$sql = $this->conn->prepare("SELECT * FROM n_list WHERE n_id = :id");
$sql->bindparam(":id", $id);
$sql->execute();
while($rows = $sql->fetch(PDO::FETCH_ASSOC))
{
echo 'Title: '.$rows["title"].' <br></br>';
echo $rows["user"];
}
Currently it gives me this:
Title: Title1
User1
Title: Title1
User2
Title: Title1
User3
I need this as a result:
Title: Title1
User1
User2
User3
How do I get the title row before while loop? Do I add another fetch there or I add another query?
I am new to pdo so sorry if the solution looks obvious to you!