使用 PHP PDO 的主从结构
我一直在尝试创建一个结果页面,其中列出了基于一系列母版的详细信息。
这是一个示例问题...
三个表,如下所示:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我宁愿将一些状态存储在 php 变量中,而不是多个查询。在这种情况下,您需要知道的是:这本书是否“已更改”,即脚本必须打印标题吗?
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?