Uko ndani ya app. Fungua kwenye browser kwa muonekano na downloads bora.
May 13, 2026 1 min read

Jinsi ya Kuunganisha Database kwa PDO kutumia PHP – Mwongozo Rahisi na Salama

Jifunze jinsi ya kufanya database connection kwa kutumia PDO katika PHP kwa njia salama, rahisi na professional. Mwongozo huu unafaa kwa beginners na developers wanaotengeneza website za kisasa.

MySQL Database
CREATE DATABASE pdo_blog;

USE pdo_blog;

CREATE TABLE posts (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
description TEXT NOT NULL,
content LONGTEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
File: config.php

setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

echo "Database connected successfully";

} catch (PDOException $e) {

die("Connection failed: " . $e->getMessage());

}

?>
File: index.php

prepare($sql);

$stmt->execute([
':title' => $title,
':description' => $description,
':content' => $content
]);

header("Location: index.php");
exit();
}

$posts = $pdo->query("SELECT * FROM posts ORDER BY id DESC");

?>

PDO Blog Tutorial

Add Blog Post

All Blog Posts

= htmlspecialchars($post['title']) ?>

= date('d M Y', strtotime($post['created_at'])) ?>

= htmlspecialchars($post['description']) ?>

= nl2br(htmlspecialchars($post['content'])) ?>

© PDO Blog Tutorial

Features za Code
PDO Secure Database Connection
Prepared Statements
SQL Injection Protection
Bootstrap Design
Add Unlimited Blog Posts
Responsive Design
Professional Layout
Simple CRUD Foundation
Website Link Example

https://aulink.com

🚀 Unahitaji mfumo au website ya biashara?

Chagua huduma hapa chini kisha mteja bofya moja kwa moja kwenda kwenye ukurasa wa huduma au kuwasiliana nasi kwa WhatsApp.

Share this post

Comments

0
No comments yet. Be the first to comment.

Continue Reading

Subscribe

Get new updates

Jiunge upokee posts mpya, tutorials, na updates za mifumo moja kwa moja kwenye email yako.

Quick Actions