Proje sonu - Blog (PDO)
Tüm parçaları birleştirip PDO'lu çalışan blogu tamamlarsın.
Tüm parçaları birleştirmek
Son derste tüm parçaları birleştiren bir runBlog() fonksiyonu yazacaksın. Bu fonksiyon setupDatabase() ile bağlanır, createPost() ile birkaç yazı ekler, getAllPosts() ile sayıyı yazdırır, getPost() ile tekil yazıyı gösterir, updatePost() ile bir yazıyı günceller, deletePost() ile başka birini siler ve son olarak kalan yazıları listeler.
Bu demo, PDO ile yazdığın tüm CRUD işlemlerinin uçtan uca nasıl çalıştığını gösterir. Çıktı deterministiktir çünkü sqlite::memory: her çalıştırmada temiz başlar.
Deterministik çıktı
Bellek içi veritabanı her çalıştırmada temiz başladığı için runBlog() çıktısı her zaman aynıdır. Üç yazı eklersin, ikincisini okur ve güncellersin, üçüncüyü silersin; sonuçta iki yazı kalır ve bunları başlık ve yazarla yazdırırsın.
Bu kalıp gerçek bloglarda da geçerlidir: bağlan, veri ekle, oku, düzenle, sil ve son durumu göster. PDO bu adımları güvenli ve tutarlı bir şekilde yapmanı sağlar.
Uygulama runBlog fonksiyonunu yaz
Bir runBlog(): void fonksiyonu yaz ve çağır. İçinde $pdo = setupDatabase(); ile başla. Üç yazı ekle (createPost). getAllPosts ile sayıyı echo "Toplam: " . count(...) . "\n"; şeklinde yazdır. getPost($pdo, 2) ile ikinci yazının başlığını yazdır. updatePost ile ikinci yazıyı güncelle, deletePost ile üçüncüyü sil. Son olarak getAllPosts ile kalanları foreach ile yazdır. En altta runBlog(); çağrısı yapsın.
Örnek çözümü göster
void { ... } yaz. İçinde $pdo = setupDatabase(); ile başla, createPost ile üç yazı ekle, getAllPosts ile sayıyı yazdır, getPost(2) ile ikinci yazının başlığını yazdır, updatePost ile ikinci yazıyı güncelle, deletePost ile üçüncü yazıyı sil, sonra getAllPosts ile kalanları foreach ile yazdır. En son runBlog(); çağrısı yap.
Örnek çözüm:
<?php
// Blog projesi: tüm parçalar birleşti — runBlog demo'sunu yaz
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, "Blog açılışı", "Merhaba dünya", "Ayşe");
createPost($pdo, "İkinci yazı", "PHP PDO öğreniyorum", "Alex");
createPost($pdo, "Üçüncü yazı", "Veritabanı pratik", "Merve");
$posts = getAllPosts($pdo);
echo "Toplam: " . count($posts) . "\n";
$post = getPost($pdo, 2);
echo $post['title'] . "\n";
updatePost($pdo, 2, "Güncellenmiş yazı", "Yeni içerik", "Alex");
deletePost($pdo, 3);
$posts = getAllPosts($pdo);
foreach ($posts as $p) {
echo $p['title'] . " - " . $p['author'] . "\n";
}
}
runBlog();
Kendini test et
-
runBlog fonksiyonu neyi gösterir?
- Tüm CRUD işlemlerini birleştiren tam bir blog akışını
- Yalnızca veritabanı bağlantısını
- Sadece HTML çıktısını
- Tek bir SQL sorgusunu
Cevabı göster
Tüm CRUD işlemlerini birleştiren tam bir blog akışını — Doğru. runBlog setupDatabase, createPost, getAllPosts, getPost, updatePost ve deletePost'u birleştirir.
-
Blog akışını eşleştir
- setupDatabase
- Tablo oluşturur
- createPost
- Yazı ekler
- getPost
- Tek yazı getirir
- updatePost
- Yazıyı günceller
- deletePost
- Yazıyı siler
-
DELETE sorgusunu tamamla
$stmt = $pdo->prepare("____");Cevabı göster
$stmt = $pdo->prepare("DELETE FROM posts WHERE id = :id");