Project: Blog with PDO
Overview of the 5-lesson PDO blog project: post management with PDO.
Project: Blog with PDO
In this 5-lesson project you'll build a blog with PDO. Each lesson adds one piece, and by the final lesson you'll complete a working mini application.
You'll use what you learned in previous groups: PDO connections, prepared statements and CRUD operations. Throughout the project you'll design a BlogPost class that holds title, content and author. Then you'll add setupDatabase(), createPost(), getAllPosts(), getPost(), updatePost() and deletePost() functions.
Project roadmap
The project has five lessons. In the second lesson you'll connect an in-memory SQLite database with setupDatabase() and create the posts table. In the third you'll add posts with createPost() and list them with getAllPosts(), using prepare and execute. In the fourth you'll read, edit and delete single posts with getPost(), updatePost() and deletePost(). In the final lesson you'll write a runBlog() demo that combines all parts.
Exercise Define the BlogPost class
Write the first piece of this project. Define a class named BlogPost. Add public string $title = "";, public string $content = ""; and public string $author = ""; properties. Then create an instance with $post = new BlogPost();, set the title with $post->title = "First post"; and print with echo $post->title;.
Show example solution
Declare the class with class BlogPost { ... }. Add public string $title = "";, public string $content = ""; and public string $author = "". Then create an instance with $post = new BlogPost();, set the title with $post->title = "First post"; and print with echo $post->title;.
Example solution:
<?php
// Skeleton class for the blog project
class BlogPost {
public string $title = "";
public string $content = "";
public string $author = "";
}
$post = new BlogPost();
$post->title = "First post";
echo $post->title;
Test yourself
-
Why should we use PDO for the blog project?
- PDO connects to many databases with one API and offers secure queries
- Because PDO runs faster
- PDO works only with SQLite
- PDO generates HTML automatically
Show answer
PDO connects to many databases with one API and offers secure queries — Correct. PDO offers a consistent API and protects against SQL injection with prepared statements.
-
Complete the class name
class ____ { public string $title = ""; public string $content = ""; public string $author = ""; }Show answer
class BlogPost { public string $title = ""; public string $content = ""; public string $author = ""; } -
Match the project concepts
- BlogPost
- Post data
- setupDatabase
- Creates the table
- createPost
- Adds a post
- getAllPosts
- Lists all posts