Managing Tables
Creating and Populating Tables
Managing Tables is a free PHP Academy lesson on CoddyKit — lesson 3 of 6. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 6 lessons in the course, and your progress syncs across the web and the CoddyKit app.
1
Creating and Populating Tables
Welcome to the next lesson! In this lesson, you’ll learn how to create tables in a MySQL database and populate them with data using PHP. Let’s build your database structure!

2
What Are Tables in a Database?
Tables are the building blocks of relational databases. They organize data into rows and columns:
- Rows: Represent individual records.
- Columns: Represent fields in each record.
Example Table: A table for storing user information:
| ID | Name | Email |
|----|----------|-----------------|
| 1 | Alice | alice@mail.com |
| 2 | Bob | bob@mail.com |
3
Creating a Table Using SQL
Use the CREATE TABLE statement to define the structure of your table. Example:
Explanation:
id: A unique identifier for each record.nameandemail: Fields for user information.created_at: Timestamp for when the record was created.
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
email VARCHAR(50) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
4
Creating Tables with PHP
Use PHP to execute the CREATE TABLE statement. Example:
Key Point: Use the exec() method for statements that don’t return results.
<?php
$sql = "CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
email VARCHAR(50) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)";
try {
$pdo->exec($sql);
echo "Table created successfully!";
} catch (PDOException $e) {
echo "Error creating table: " . $e->getMessage();
}
?>
5
Inserting Data into a Table
Use the INSERT INTO statement to add records to your table. Example:
Tip: Use placeholders (:name, :email) to prevent SQL injection.
<?php
$sql = "INSERT INTO users (name, email) VALUES (:name, :email)";
$stmt = $pdo->prepare($sql);
$stmt->execute(["name" => "Alice", "email" => "alice@mail.com"]);
echo "Data inserted successfully!";
?>
6
Inserting Multiple Records
Use a loop to insert multiple records into your table. Example:
Tip: Batch processing is efficient for inserting multiple records.
<?php
$users = [
["name" => "Bob", "email" => "bob@mail.com"],
["name" => "Charlie", "email" => "charlie@mail.com"]
];
$sql = "INSERT INTO users (name, email) VALUES (:name, :email)";
$stmt = $pdo->prepare($sql);
foreach ($users as $user) {
$stmt->execute($user);
}
echo "Multiple records inserted successfully!";
?>
7
Viewing Data in a Table
After populating your table, use the SELECT statement to view the data. Example:
Key Point: Use the PDO::FETCH_ASSOC mode to fetch data as an associative array.
<?php
$sql = "SELECT * FROM users";
$stmt = $pdo->query($sql);
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "ID: " . $row["id"] . ", Name: " . $row["name"] . ", Email: " . $row["email"] . "<br>";
}
?>
8
Handling Errors
Use try-catch blocks to handle errors when creating or populating tables. Example:
Tip: Log errors for debugging instead of displaying them to users.
<?php
try {
$stmt = $pdo->prepare($sql);
$stmt->execute(["name" => "Dave", "email" => "dave@mail.com"]);
echo "Data inserted successfully!";
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}
?>
9
10
Great Job!
Congratulations! You’ve learned how to create and populate tables in a MySQL database using PHP. In the next lesson, we’ll explore querying data with advanced SQL techniques. Let’s keep coding!

Frequently asked questions
Is the “Managing Tables” lesson free?
Yes — the full text of “Managing Tables” is free to read here on the web, and the PHP Academy course includes 6 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.
What will I learn in “Managing Tables”?
Creating and Populating Tables You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start PHP Academy?
No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 6, so you can start here or from the beginning and move at your own pace.
How long does the “Managing Tables” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this PHP Academy lesson?
Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.