PHP PDO MYSQL 连接

发布于 2024-08-12 18:48:47 字数 576 浏览 1 评论 0原文

我有一个名为 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 技术交流群。

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

发布评论

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

评论(2

梦里的微风 2024-08-19 18:48:47

如何连接两个表:

SELECT id, title, slug, cms_page_part.* FROM cms_page JOIN cms_page_part ON cms_page.id = cms_page_part.page_id WHERE cms_page.id=$id

我在评论中所说的示例:

// Connect to database
$conn = new PDO(DB_DSN, DB_USER, DB_PASS);

// Page ID
$id = (int) $_GET['id'];

// Fetch page
$pageStmt = $conn->query(sprintf('SELECT * FROM cms_page WHERE id=%d', $id));
if ($pageStmt->fetchColumn() != 0) {
    $page = $pageStmt->fetch(PDO::FETCH_ASSOC);

    // Fetch parts
    $partsStmt = $conn->query(sprintf('SELECT * FROM cms_page_parts WHERE page_id=%d', $id));
    while ($part = $partsStmt->fetch(PDO::FETCH_ASSOC)) {
        // ...
    }
    $partsStmt->closeCursor();
}
$pageStmt->closeCursor();

示例(带有准备好的语句):

// Connect to database
$conn = new PDO(DB_DSN, DB_USER, DB_PASS);

// Page ID
$id = (int) $_GET['id'];

// Fetch page
$pageStmt = $conn->query('SELECT * FROM cms_page WHERE id=:id');
if ($pageStmt->execute(array(':id' => $id))) {
    if ($pageStmt->fetchColumn() != 0) {
        $page = $pageStmt->fetch(PDO::FETCH_ASSOC);

        // Fetch parts
        $partsStmt = $conn->query('SELECT * FROM cms_page_parts WHERE page_id=:id');
        if ($partsStmt->execute(array(':id' => $id))) {
            while ($part = $partsStmt->fetch(PDO::FETCH_ASSOC)) {
                // ...
            }
            $partsStmt->closeCursor();
        }
    }
    $pageStmt->closeCursor();
}

How to JOIN the two tables:

SELECT id, title, slug, cms_page_part.* FROM cms_page JOIN cms_page_part ON cms_page.id = cms_page_part.page_id WHERE cms_page.id=$id

Example of what I said in my comments:

// Connect to database
$conn = new PDO(DB_DSN, DB_USER, DB_PASS);

// Page ID
$id = (int) $_GET['id'];

// Fetch page
$pageStmt = $conn->query(sprintf('SELECT * FROM cms_page WHERE id=%d', $id));
if ($pageStmt->fetchColumn() != 0) {
    $page = $pageStmt->fetch(PDO::FETCH_ASSOC);

    // Fetch parts
    $partsStmt = $conn->query(sprintf('SELECT * FROM cms_page_parts WHERE page_id=%d', $id));
    while ($part = $partsStmt->fetch(PDO::FETCH_ASSOC)) {
        // ...
    }
    $partsStmt->closeCursor();
}
$pageStmt->closeCursor();

Example (with prepared statements):

// Connect to database
$conn = new PDO(DB_DSN, DB_USER, DB_PASS);

// Page ID
$id = (int) $_GET['id'];

// Fetch page
$pageStmt = $conn->query('SELECT * FROM cms_page WHERE id=:id');
if ($pageStmt->execute(array(':id' => $id))) {
    if ($pageStmt->fetchColumn() != 0) {
        $page = $pageStmt->fetch(PDO::FETCH_ASSOC);

        // Fetch parts
        $partsStmt = $conn->query('SELECT * FROM cms_page_parts WHERE page_id=:id');
        if ($partsStmt->execute(array(':id' => $id))) {
            while ($part = $partsStmt->fetch(PDO::FETCH_ASSOC)) {
                // ...
            }
            $partsStmt->closeCursor();
        }
    }
    $pageStmt->closeCursor();
}
风铃鹿 2024-08-19 18:48:47
$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'");
$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'");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文