PHP CRUD Operations
In this page:
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
// 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";
?>
Login to try C/C++/Java/PHP code in the editor
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
$db = new SQLite3(':memory:');
$db->exec("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)");
$stmt = $db->prepare("INSERT INTO users (name) VALUES (:name)");
$stmt->bindValue(':name', 'Alice', SQLITE3_TEXT);
$stmt->execute();
echo "New row inserted";
?>
Login to try C/C++/Java/PHP code in the editor
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
$db = new SQLite3(':memory:');
$db->exec("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)");
$db->exec("INSERT INTO users (name) VALUES ('Alice')");
$result = $db->query("SELECT * FROM users WHERE id = 1");
print_r($result->fetchArray());
?>
Login to try C/C++/Java/PHP code in the editor
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
$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");
echo "Row updated";
?>
Login to try C/C++/Java/PHP code in the editor
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
$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");
echo "Row deleted";
?>
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 21 topics to unlock
0/21 topics done
Complete these topics first:
- PHP MySQL Introduction
- PHP MySQLi Connection
- PHP PDO Introduction
- PHP CRUD Operations
- PHP Prepared Statements
- PHP Stored Procedures
- PHP Transactions
- PHP Error Handling in DB
- PHP MySQL Connect
- PHP MySQL Create DB
- PHP MySQL Create Table
- PHP MySQL Insert Data
- PHP MySQL Get Last ID
- PHP MySQL Insert Multiple
- PHP MySQL Prepared Statements
- PHP MySQL Select Data
- PHP MySQL Where
- PHP MySQL Order By
- PHP MySQL Delete Data
- PHP MySQL Update Data
- PHP MySQL Limit Data