" . $row["title"] . "
"; echo "" . $row["content"] . "
"; echo "by " . $row["author"] . " on " . $row["created_at"] . "
"; echo "Creating a blog using PHP and MySQL is a great project to get hands-on experience with web development. Here’s a basic outline to get you started:
blog
.admin
: For admin backend files.includes
: For reusable pieces of code.static
: For static files like CSS, JavaScript, and images.Create a MySQL database and a table for your blog posts. Here’s a simple SQL script to create a posts
table:
CREATE DATABASE blog_db;
USE blog_db;
CREATE TABLE posts (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
content TEXT NOT NULL,
author VARCHAR(100),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Create an index.php
file in your project root:
num_rows > 0) { while($row = $result->fetch_assoc()) { echo "
" . $row["content"] . "
"; echo "by " . $row["author"] . " on " . $row["created_at"] . "
"; echo ""; } } else { echo "0 results"; } $conn->close(); ?> " style="position: relative; display: block; border: 1px solid var(--cib-color-stroke-neutral-primary); border-radius: var(--cib-border-radius-large); clear: both; margin-top: 12px; margin-block: 24px 12px; color: rgb(17, 17, 17); font-family: -apple-system, Roboto, SegoeUI, "Segoe UI", "Helvetica Neue", Helvetica, "Microsoft YaHei", "Meiryo UI", Meiryo, "Arial Unicode MS", sans-serif; font-size: 16px;">
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "blog_db";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, title, content, author, created_at FROM posts";
$result = $conn->query($sql);
?>
<!DOCTYPE html>
<html>
<head>
<title>My Blog</title>
<link rel="stylesheet" type="text/css" href="static/css/style.css">
</head>
<body>
<h1>Welcome to My Blog</h1>
<?php
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<div class='post'>";
echo "<h2>" . $row["title"] . "</h2>";
echo "<p>" . $row["content"] . "</p>";
echo "<p><em>by " . $row["author"] . " on " . $row["created_at"] . "</em></p>";
echo "</div>";
}
} else {
echo "0 results";
}
$conn->close();
?>
</body>
</html>
Create an admin
folder and add files for creating, editing, and deleting posts. For example, create_post.php
:
<?php
// Include database connection
include('../includes/db_connect.php');
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$title = $_POST['title'];
$content = $_POST['content'];
$author = $_POST['author'];
$sql = "INSERT INTO posts (title, content, author) VALUES ('$title', '$content', '$author')";
if ($conn->query($sql) === TRUE) {
echo "New post created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Create Post</title>
</head>
<body>
<h1>Create a New Post</h1>
<form method="post" action="create_post.php">
<label for="title">Title:</label><br>
<input type="text" id="title" name="title"><br>
<label for="content">Content:</label><br>
<textarea id="content" name="content"></textarea><br>
<label for="author">Author:</label><br>
<input type="text" id="author" name="author"><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
This is a basic setup to get you started. You can find more detailed tutorials and complete projects on platforms like GitHub1 and CodeWithAwa2. Happy coding! 😊