Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6097673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:04:14+00:00 2026-05-23T13:04:14+00:00

I’m stuck trying to create a result page that lists details based on a

  • 0

I’m stuck trying to create a result page that lists details based on a series of masters.

Here’s a sample problem…

Three tables such as those shown below:

Book (id, title)
Author (id, name)
BookAuthors (bookID, authorID)

I wish to generate a list similar to this:

Book Title 1
  Author 1
  Author 2
Book Title 2
  Author 3
Book Title 3
  Author 1
  Author 4

and so on.

I wish to use PHP PDO to achieve this.

[Updated with more detail]

The problem I have is more about how to go about traversing the results from the PDO when nesting prepared statements is not possible…

I was hoping to nest a query within a query and use the results from the 1st query [I’ve extended the query to include a filter on the book titles, but this may be blank thus returning the whole set]:

select id as idBook,title from book where title like %:title%

to then create the list of authors for each book

select name from author, bookauthors 
  where bookauthor.authorid = author.id 
    AND bookauthor.bookid = :idBook:

Where :idBook: is the next book id from the result set from the 1st query.

As the number of books retrieved in the 1st query may be large, I was hoping not to use fetchall if possible…

Anyone got pointers / sample solution code for this?

many thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T13:04:15+00:00Added an answer on May 23, 2026 at 1:04 pm

    Instead of multiple queries I’d rather store some state in a php variable. In this case all you need to know is: has the book “changed”, i.e. must the script print the title?

    <?php
    $pdo = new PDO('mysql:host=localhost;dbname=test', 'localonly', 'localonly', array(PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION));
    foo($pdo);  // add test data
    
    $r = $pdo->query('
      SELECT
        l.bookID, b.title, a.name
      FROM
        BookAuthor as l
      JOIN
        Author as a
      ON
        a.id=l.authorID 
      JOIN
        Book as b
      ON
        b.id=l.bookID
      ORDER BY
        l.bookID,
        l.authorID
    ');
    $currentBook = null;
    foreach($r as $row) {
      if ( $currentBook!=$row['bookID'] ) {
        echo $row['title'], "\n";
        $currentBook=$row['bookID'];
      }
      echo '  ', $row['name'], "\n";
    }
    
    function foo($pdo) {
      $pdo->exec('create temporary table Book (id int auto_increment, title varchar(48), primary key(id))');
      $pdo->exec('create temporary table Author (id int auto_increment, name varchar(48), primary key(id))');
      $pdo->exec('create temporary table BookAuthor (bookID int, authorID int)');
    
      $pdo->exec("INSERT INTO Author (id,name) VALUES (1,'Author1'),(2,'Author2'),(3,'Author3'),(4,'Author4')");
      $pdo->exec("INSERT INTO Book (id,title) VALUES (1,'Book Title 1'),(2,'Book Title 2'),(3,'Book Title 3')");
      $pdo->exec("INSERT INTO BookAuthor (bookID,authorID) VALUES (1,1),(1,2),(2,3),(3,1),(3,4)");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.