PHP + a Database = a Dynamic Website
The final piece: where the data actually lives
Here is the puzzle this whole course has been building toward. You can display data, loop over it, and receive it from forms. But when your PHP script finishes running, its variables vanish. So where does a real website keep ten thousand products and a million comments between page loads? In a database.
What a database is
A database is a program whose entire job is storing data and finding it again, fast. It usually runs right alongside PHP on the server. The most popular one in the PHP world is MySQL, and the two have been paired together for so long that huge portions of the web run on exactly this duo.
Data in MySQL lives in tables, which look pleasantly like spreadsheets: a products table might have columns called name, price, and stock, with each row holding one product. To talk to the database, you write short requests in a language called SQL. It reads almost like English: SELECT name, price FROM products means fetch me the name and price of every row in the products table.
The pattern that powers the web
Every dynamic page you have ever used, every store, feed, forum, and blog, boils down to one three-step pattern:
- PHP asks the database a question.
- The database hands back rows of data.
- PHP loops over the rows and turns each one into HTML.
And here is the wonderful part: you already know every skill this requires. Look at real PHP database code, using PDO, PHP's built-in tool for talking to databases:
<?php
$db = new PDO("mysql:host=localhost;dbname=shop", "user", "password");
$rows = $db->query("SELECT name, price FROM products")->fetchAll();
foreach ($rows as $product) {
echo "<li>" . htmlspecialchars($product["name"]) . " - $" . $product["price"] . "</li>";
}
?>Walk through it and watch your whole course click together. The database hands back $rows, which is a numbered array of associative arrays, the exact shape you learned in module two. The foreach is the loop from module three. Reading $product["name"] is associative array access. And htmlspecialchars is there because database data counts as outside data, the rule from last lesson. Nothing here is new; it is your skills, assembled.
The payoff is enormous: those few lines produce a list of 5 products or 50,000. Add a product to the database and the page updates itself on the next visit, no code changes. One PHP file plus one table can replace thousands of hand-written HTML pages. That is what dynamic actually means, and it is how WordPress can run millions of completely different blogs with one shared codebase.
One safety note for the road
When a query needs to include user input, say, a search term from a form, never glue it into the SQL string by hand. That opens the door to an attack called SQL injection, the database cousin of XSS. PDO provides prepared statements, which send the query and the user's data separately so the data can never be mistaken for SQL commands. When you reach your first database course, you will learn them properly; for now, just remember the phrase.
Where you are now
Take stock of what you can do: embed PHP in HTML, store and shape data with variables and arrays, make decisions, loop, write functions, receive form input, escape output like a professional, and read real database code and understand every line. That is not a party trick, that is the genuine foundation of server-side web development.
You made it! From here, natural next steps are a hands-on SQL and database course, building a tiny guestbook or to-do list as your first full project, and eventually exploring how tools like WordPress and Laravel build on exactly the PHP you now know.
Quick check
Q1 In the classic dynamic-website pattern, what does PHP do with the rows a database sends back?
Q2 Why do query results from the database feel familiar after this course?
Want to save your progress? It's free.
Create a free account