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

PHP CRUD Operations

CRUD का मतलब है create, read, update, और delete, वे चार चीज़ें जो आप information के साथ कर सकते हैं। यह एक library जैसा है जहाँ आप एक book add कर सकते हैं, एक ढूँढ सकते हैं, उसका label fix कर सकते हैं, या उसे हटा सकते हैं।
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 का मतलब है Create, Read, Update, Delete — चार fundamental operations जो कोई भी application stored data पर perform करती है, और वे सीधे SQL के INSERT, SELECT, UPDATE, और DELETE statements पर map होते हैं।

उदाहरण: 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 एक INSERT statement इस्तेमाल करती हैं, आमतौर पर bound parameters वाले एक prepared statement के through, किसी table में एक नया row — जैसे एक नया user record — add करने के लिए।

उदाहरण: 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 मौजूदा data fetch करने के लिए SELECT statements इस्तेमाल करती हैं, ID से एक single record से लेकर query के WHERE और ORDER BY clauses के आधार पर कई records की एक filtered, sorted list तक।

उदाहरण: 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 एक UPDATE statement इस्तेमाल करती हैं जिसमें exactly कौन सा row(s) बदलना है यह identify करने वाला एक WHERE clause होता है — WHERE clause भूल जाना एक classic, dangerous mistake है जो table के हर row को update कर देती है।

उदाहरण: 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 एक DELETE statement इस्तेमाल करती हैं, और UPDATE की तरह, एक गलती से छूटा हुआ WHERE clause हर row हटा देगा — कई teams इससे बचाव के लिए एक 'soft delete' pattern (row को हटाने के बजाय inactive mark करना) बनाती हैं।

उदाहरण: 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. #}
आम गलतियां
  1. एक UPDATE या DELETE statement में WHERE clause भूल जाना, जो table के हर row को बदल या हटा देता है।
  2. user input concatenate करके SQL बनाना, जो SQL injection की अनुमति देता है; prepared statements इस्तेमाल करें।
  3. यह check न करना कि query succeed हुई, ताकि एक failed insert या update 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.