使用 PHP PDO 的主从结构

发布于 2024-11-17 16:25:53 字数 878 浏览 1 评论 0原文

我一直在尝试创建一个结果页面,其中列出了基于一系列母版的详细信息。

这是一个示例问题...

三个表,如下所示:

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

我希望生成一个与此类似的列表:

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

等等。

我希望使用 PHP PDO 来实现这一点。

[更新更多细节]

我遇到的问题更多是关于当不可能嵌套准备好的语句时如何遍历 PDO 的结果...

我希望在查询中嵌套查询并使用第一个查询的结果查询 [我已经扩展了查询以包含书名过滤器,但这可能是空白的,因此返回整个集合]:

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

然后为每本书创建作者列表

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

其中:idBook:是来自从第一组开始的结果集 询问。

由于第一个查询中检索到的书籍数量可能很大,我希望如果可能的话不要使用 fetchall...

有人有这方面的指针/示例解决方案代码吗?

非常感谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

铃予 2024-11-24 16:25:53

我宁愿将一些状态存储在 php 变量中,而不是多个查询。在这种情况下,您需要知道的是:这本书是否“已更改”,即脚本必须打印标题吗?

<?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)");
}

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)");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文