CodeBlanks Logo Icon CodeBlanks

Project final - Blog with PDO

PHP course · Advanced · Lesson 26/92

Combine all parts and finalize a working blog with PDO.

Combining all parts

In the final lesson you'll write a runBlog() function that combines all parts. It connects with setupDatabase(), adds a few posts with createPost(), prints the count with getAllPosts(), shows one post with getPost(), updates a post with updatePost(), deletes another with deletePost() and finally lists the remaining posts.

This demo shows how all the CRUD operations you wrote with PDO work end to end. The output is deterministic because sqlite::memory: starts clean on every run.

Deterministic output

Because the in-memory database starts clean on every run, the runBlog() output is always the same. You add three posts, read and update the second, delete the third; two posts remain and you print them with their title and author.

This pattern holds in real blogs too: connect, add data, read, edit, delete and show the final state. PDO lets you do these steps safely and consistently.

Exercise Write the runBlog function

Write a runBlog(): void function and call it. Inside, start with $pdo = setupDatabase();. Add three posts (createPost). Print the count with getAllPosts as echo "Total: " . count(...) . "\n";. Print the second post's title with getPost($pdo, 2). Update the second post with updatePost, delete the third with deletePost. Finally list the remaining posts with getAllPosts and foreach. Add a runBlog(); call at the bottom.

Show example solution
void { ... }. Inside, start with $pdo = setupDatabase();, add three posts with createPost, print the count with getAllPosts, print the second post's title with getPost(2), update the second post with updatePost, delete the third with deletePost, then list the remaining posts with getAllPosts and foreach. Finally call runBlog();.
Example solution:
<?php
// Blog project: all parts combined — write the runBlog demo

class BlogPost {
    public string $title = "";
    public string $content = "";
    public string $author = "";
}

function setupDatabase(): PDO {
    $pdo = new PDO("sqlite::memory:");
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $pdo->exec("CREATE TABLE posts (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        title TEXT NOT NULL,
        content TEXT NOT NULL,
        author TEXT NOT NULL
    )");
    return $pdo;
}

function createPost(PDO $pdo, string $title, string $content, string $author): int {
    $stmt = $pdo->prepare("INSERT INTO posts (title, content, author) VALUES (:title, :content, :author)");
    $stmt->execute([":title" => $title, ":content" => $content, ":author" => $author]);
    return (int) $pdo->lastInsertId();
}

function getAllPosts(PDO $pdo): array {
    $stmt = $pdo->query("SELECT id, title, content, author FROM posts ORDER BY id");
    return $stmt->fetchAll(PDO::FETCH_ASSOC);
}

function getPost(PDO $pdo, int $id): array {
    $stmt = $pdo->prepare("SELECT id, title, content, author FROM posts WHERE id = :id");
    $stmt->execute([":id" => $id]);
    return $stmt->fetch(PDO::FETCH_ASSOC) ?: [];
}

function updatePost(PDO $pdo, int $id, string $title, string $content, string $author): int {
    $stmt = $pdo->prepare("UPDATE posts SET title = :title, content = :content, author = :author WHERE id = :id");
    $stmt->execute([":title" => $title, ":content" => $content, ":author" => $author, ":id" => $id]);
    return $stmt->rowCount();
}

function deletePost(PDO $pdo, int $id): int {
    $stmt = $pdo->prepare("DELETE FROM posts WHERE id = :id");
    $stmt->execute([":id" => $id]);
    return $stmt->rowCount();
}

function runBlog(): void {
    $pdo = setupDatabase();
    createPost($pdo, "Opening post", "Hello world", "Alex");
    createPost($pdo, "Second post", "Learning PHP PDO", "Mary");
    createPost($pdo, "Third post", "Database practice", "Sam");

    $posts = getAllPosts($pdo);
    echo "Total: " . count($posts) . "\n";

    $post = getPost($pdo, 2);
    echo $post['title'] . "\n";

    updatePost($pdo, 2, "Updated post", "New content", "Mary");
    deletePost($pdo, 3);

    $posts = getAllPosts($pdo);
    foreach ($posts as $p) {
        echo $p['title'] . " - " . $p['author'] . "\n";
    }
}

runBlog();

Test yourself

  1. What does the runBlog function demonstrate?

    • A full blog flow that combines all CRUD operations
    • Only the database connection
    • Only HTML output
    • A single SQL query
    Show answer

    A full blog flow that combines all CRUD operations — Correct. runBlog combines setupDatabase, createPost, getAllPosts, getPost, updatePost and deletePost.

  2. Match the blog flow

    setupDatabase
    Creates the table
    createPost
    Adds a post
    getPost
    Fetches one post
    updatePost
    Updates a post
    deletePost
    Deletes a post
  3. Complete the DELETE query

    $stmt = $pdo->prepare("____");
    Show answer
    $stmt = $pdo->prepare("DELETE FROM posts WHERE id = :id");

All lessons in Advanced