45-Minute PHP and SQLite Workshop
In 45 minutes, learners run a reading list, follow one form submission through PHP and SQLite, and change how the app lists books. They should be able to edit a file, open a terminal, and follow a short command. The workshop uses one local PHP file and a browser.
Prepare before the session
Section titled “Prepare before the session”Send learners the PHP and SQLite setup instructions and ask them to run the starter before the session. Try the same steps on a machine with the PHP installation you’ll demonstrate.
- Check the driver with
php -r 'print_r(PDO::getAvailableDrivers());'and look forsqlite. - Start the app with
php -S 127.0.0.1:8000 -t publicfrom the starter project folder. Openhttp://127.0.0.1:8000/. - Keep a copy of the starter file for pairs who need to reset their code. To give a pair a blank reading list, save the file as
public/index.phpin a new project folder and start the server from there. - Let people with a setup blocker work in pairs. The instructor can follow the missing-driver guide after the session.
Run the lesson
Section titled “Run the lesson”| Time | Activity | What learners should see |
|---|---|---|
| 0–5 min | Run the starter and open the page. | The reading list page loads. |
| 5–12 min | Add First Book and Second Book. Mark Second Book as read. | The list shows both books. Second Book has a (Read) label. |
| 12–22 min | Follow the request diagram. Add a third title with Network open. | The POST returns 303, then the GET returns 200 and renders the title. |
| 22–34 min | Complete exercise 1 below. Compare results in pairs. | Unread titles come first, then read titles. |
| 34–42 min | Complete exercise 2 or explain the query change to a partner. | A visible count matches the unread titles. |
| 42–45 min | Stop and restart the server. Refresh and share one sentence about what SQLite stores. | Books and their read status persist. |
Exercise 1 — Put unread books first
Section titled “Exercise 1 — Put unread books first”Find the query containing ORDER BY id DESC near the end of the PHP block. Keep all the rows, but change the order so unread books come before read books. Keep newest first inside each group.
Mark the newest book as read to check your result. It moves below the unread books and keeps its (Read) label.
Show the solution
SELECT id, title, is_read FROM books ORDER BY is_read ASC, id DESCSQLite stores 0 for unread and 1 for read in this app. Sorting by is_read puts the zeroes first. id DESC orders each group separately.
Exercise 2 — Count unread books
Section titled “Exercise 2 — Count unread books”Find the line that assigns $books. After that line, count books with is_read equal to zero. In the HTML, display the count below the Books heading.
Check the count by marking an unread book as read. The count falls by one. When all books are read, it shows zero.
Show the solution
Add the loop after the SELECT.
$unread = 0;foreach ($books as $book) { if ((int) $book['is_read'] === 0) { $unread++; }}Print the count below the <h2>Books</h2> heading.
<p>Unread books: <?= $unread ?></p>Check understanding
Section titled “Check understanding”Ask each learner to point to the form, the INSERT, the SELECT, and the part of the HTML that prints a title. Then ask which request saves a change and which displays it. A complete answer names the POST and its redirect, the SQLite file, and the follow-up GET. Learners who finish early can choose a next project.