PHP PDO MYSQL 连接
我有一个名为 cms_page 的表,其中每个页面都有一个 ID。我想从该表中提取所有信息以及从 cms_page_part 表中提取所有信息,其中 page_id 等于我发送到页面的 ID...我知道这很令人困惑,但这是我的尝试:
require '../../config.php';
$conn = new PDO(DB_DSN, DB_USER, DB_PASS);
$id = (int)$_GET['id'];
//$q = $conn->query("SELECT * FROM cms_page WHERE id=$id");
$q = $conn->query("SELECT cms_page.id, cms_page.title, cms_page.slug, cms_page_part.* FROM cms_page LEFT JOIN cms_page_part ON cms_page_part.page_id=cms_page.id WHERE cms_page.id = $id");
$project = $q->fetch(PDO::FETCH_ASSOC);
任何帮助弄清楚如何加入这些信息都会受到赞赏。
I have a table called cms_page in which each page has an ID. I want to pull all the information from this table and all the information from cms_page_part table where page_id is equal to the ID i send to the page...confusing i know but here is my attempt:
require '../../config.php';
$conn = new PDO(DB_DSN, DB_USER, DB_PASS);
$id = (int)$_GET['id'];
//$q = $conn->query("SELECT * FROM cms_page WHERE id=$id");
$q = $conn->query("SELECT cms_page.id, cms_page.title, cms_page.slug, cms_page_part.* FROM cms_page LEFT JOIN cms_page_part ON cms_page_part.page_id=cms_page.id WHERE cms_page.id = $id");
$project = $q->fetch(PDO::FETCH_ASSOC);
Any help figuring out how to join these would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如何连接两个表:
我在评论中所说的示例:
示例(带有准备好的语句):
How to JOIN the two tables:
Example of what I said in my comments:
Example (with prepared statements):