← Back to PHP Course | Chapter 11: Database | Lesson 4 of 21

PHP CRUD Operations

CRUD stands for create, read, update, and delete, the four things you can do with information. It is like a library where you can add a book, find one, fix its label, or take it away.
Syntax
php
INSERT INTO table_name (column1, column2) VALUES (value1, value2);   // Create
SELECT column1 FROM table_name WHERE condition;                       // Read
UPDATE table_name SET column1 = value WHERE condition;                // Update
DELETE FROM table_name WHERE condition;                               // Delete

Create Operation (Insert)

CRUD stands for Create, Read, Update, Delete — the four fundamental operations any application performs on stored data, and they map directly onto SQL's INSERT, SELECT, UPDATE, and DELETE statements.

Example: Create Operation (Insert)

php
<?php
// CRUD: Create, Read, Update, Delete -- maps to INSERT, SELECT, UPDATE, DELETE
$db = new SQLite3(':memory:');
$db->exec("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)");
echo "Table ready for CRUD operations";
?>

Read Operation (Select)

Create operations use an INSERT statement, typically through a prepared statement with bound parameters, to add a new row — like a new user record — to a table.

Example: Read Operation (Select)

php
<?php
// Create a new `SQLite3` instance with ':memory:', stored in `$db`
$db = new SQLite3(':memory:');
$db->exec("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)");
// Declare `$stmt`, set to `$db->prepare("INSERT INTO users (name) VALUES (:name)")`
$stmt = $db->prepare("INSERT INTO users (name) VALUES (:name)");
$stmt->bindValue(':name', 'Alice', SQLITE3_TEXT);
$stmt->execute();
// Print "New row inserted" to the output
echo "New row inserted";
?>

Update Operation (Update)

Read operations use SELECT statements to fetch existing data, ranging from a single record by ID to a filtered, sorted list of many records depending on the query's WHERE and ORDER BY clauses.

Example: Update Operation (Update)

php
<?php
// Create a new `SQLite3` instance with ':memory:', stored in `$db`
$db = new SQLite3(':memory:');
$db->exec("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)");
$db->exec("INSERT INTO users (name) VALUES ('Alice')");
// Declare `$result`, set to `$db->query("SELECT * FROM users WHERE id = 1")`
$result = $db->query("SELECT * FROM users WHERE id = 1");
// Print a human-readable dump of `$result->fetchArray()`
print_r($result->fetchArray());
?>

Delete Operation (Delete)

Update operations use an UPDATE statement with a WHERE clause identifying exactly which row(s) to change — forgetting the WHERE clause is a classic, dangerous mistake that updates every row in the table.

Example: Delete Operation (Delete)

php
<?php
// Create a new `SQLite3` instance with ':memory:', stored in `$db`
$db = new SQLite3(':memory:');
$db->exec("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)");
$db->exec("INSERT INTO users (name) VALUES ('Alice')");
$db->exec("UPDATE users SET name = 'Alicia' WHERE id = 1");
// Print "Row updated" to the output
echo "Row updated";
?>

Complete CRUD Flow

Delete operations use a DELETE statement, and like UPDATE, an accidentally omitted WHERE clause will remove every row — many teams build a 'soft delete' pattern (marking a row inactive instead of removing it) to guard against this.

Example: Complete CRUD Flow

php
<?php
// Create a new `SQLite3` instance with ':memory:', stored in `$db`
$db = new SQLite3(':memory:');
$db->exec("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)");
$db->exec("INSERT INTO users (name) VALUES ('Alice')");
$db->exec("DELETE FROM users WHERE id = 1");
// Print "Row deleted" to the output
echo "Row deleted";
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting the WHERE clause in an UPDATE or DELETE statement, which changes or removes every row in the table.
  2. Building the SQL by concatenating user input, which allows SQL injection; use prepared statements.
  3. Not checking whether the query succeeded, so a failed insert or update goes unnoticed.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.