PHP CRUD Operations
In this page:
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
// 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
// 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";
?>
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
// 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());
?>
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
// 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";
?>
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
// 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";
?>
Login to try C/C++/Java/PHP code in the editor
- Forgetting the
WHEREclause in an UPDATE or DELETE statement, which changes or removes every row in the table. - Building the SQL by concatenating user input, which allows SQL injection; use prepared statements.
- Not checking whether the query succeeded, so a failed insert or update goes unnoticed.
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