Your Cart

Loading cart...

Website Builder – Part 3: PHP + MySQL (Dynamic Websites)

In Part 1 and Part 2, we covered static websites and advanced concepts. In this part, you will learn how to build dynamic websites using PHP and MySQL. Dynamic websites store data in a database and generate pages automatically.


1. What Is a Dynamic Website?

A dynamic website generates content based on data stored in a database. Instead of writing separate files for each page, PHP fetches data from MySQL and displays it dynamically.

Examples of dynamic websites:


2. Understanding PHP and MySQL Roles

PHP communicates with MySQL to fetch, insert, update, and delete information.


3. Setting Up Database in WAMP

WAMP includes phpMyAdmin, which allows easy database management.

Steps:

  1. Open http://localhost/phpmyadmin
  2. Create a new database (example: example_db)
  3. Create tables inside the database

Example table (blog_posts):

CREATE TABLE blog_posts (
    id INT AUTO_INCREMENT PRIMARY KEY,
    title VARCHAR(255),
    content TEXT,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

4. Connecting PHP to MySQL

Use mysqli or PDO to connect PHP with MySQL.

Example database connection:

<?php
$conn = new mysqli("localhost", "root", "", "example_db");

if ($conn->connect_error) {
    die("Database connection failed");
}
?>

This file is usually placed inside an includes folder.


5. Fetching Data from Database (READ)

Fetching data allows content to appear dynamically.

<?php
$result = $conn->query("SELECT * FROM blog_posts");

while ($row = $result->fetch_assoc()) {
    echo "<h3>" . htmlspecialchars($row['title']) . "</h3>";
    echo "<p>" . htmlspecialchars($row['content']) . "</p>";
}
?>

This single code block can display unlimited posts.


6. Inserting Data (CREATE)

Dynamic websites allow users or admins to add content.

<?php
$title = $_POST['title'];
$content = $_POST['content'];

$stmt = $conn->prepare(
    "INSERT INTO blog_posts (title, content) VALUES (?, ?)"
);
$stmt->bind_param("ss", $title, $content);
$stmt->execute();
?>

Prepared statements help prevent SQL injection attacks.


7. Updating Data (UPDATE)

Editing existing data is essential for admin panels.

<?php
$stmt = $conn->prepare(
    "UPDATE blog_posts SET title=? WHERE id=?"
);
$stmt->bind_param("si", $title, $id);
$stmt->execute();
?>

8. Deleting Data (DELETE)

Deleting data must be handled carefully.

<?php
$stmt = $conn->prepare(
    "DELETE FROM blog_posts WHERE id=?"
);
$stmt->bind_param("i", $id);
$stmt->execute();
?>

9. Basic Security for Dynamic Websites

Security is more important for dynamic sites.

Never trust data coming from users directly.


10. Dynamic Website Folder Structure

/dynamic-site
 ├─ index.php
 ├─ blog.php
 ├─ admin
 │   └─ dashboard.php
 ├─ includes
 │   ├─ config.php
 │   └─ header.php
 └─ assets

This structure keeps logic organized and scalable.


Conclusion

PHP and MySQL transform a static website into a powerful dynamic system. With databases, you can manage content efficiently and build professional applications.

In the next part, we will cover user authentication and admin panels, which are essential for real-world websites.

💬