Mysql/php group books of the same author

Thank you. Could you suggest me some book/link to learn this?

EDIT

Using another code, suggested by someone of you in another thread (no more visible :blush:), I managed to get this new code, working as expected:

    require "$root/PDO_connect.php";
    
    $sql = $pdo->query("
    SELECT a.autore, a.autore_nome, b.autore, b.autore_nome, b.titolo, b.edizione, b.luogo, b.data
    FROM bibliografie__autori a
    INNER JOIN bibliografie b
    WHERE a.autore = b.autore 
    ORDER BY a.autore
    ");
    $autori = $sql->fetchAll(PDO::FETCH_GROUP);
    $posts = array();
 foreach($autori as $autore => $books) {
    echo "
     <div class='left'> 
     <h2>" . $books[0]['autore'] . "  </h2>\n
     ";
     foreach($books as $book) {
         printf("<p>[$book[data]] <i>$book[titolo]</i>, $book[edizione], $book[luogo]</p>");
    }
    echo "
    </div>\n";
 }

I have to do some improvements, but it works.

1 Like

Sorry, Iā€™m mixing up my fetch modes, I think it should be FETCH_GROUP.
Try:-

  $stmt = $pdo->query('SELECT autore, titolo, data FROM bibliografie')->fetchAll(PDO::FETCH_GROUP);
  var_dump($stmt) ; exit() ; // For testing only

Fetch Group, that is the one.

That would depend upon the application as a whole.
To fulfil only the requirement of this topic, a list of authors and their books, there is no need for another table.
If the project also required a page per author, perhaps with a bio and other information about them, plus a list of works, then I would have another table for authors.

1 Like