What is PDO in PHP?
PDO (PHP Data Objects) is a built-in PHP extension that provides a consistent interface to connect and interact with different types of databases like MySQL, PostgreSQL,
SQLite, and more.
Why use PDO?
Security: PDO uses prepared statements, which protect your database from SQL injection attacks one of the most common web vulnerabilities.
Database flexibility: With PDO, you can easily switch the database driver without changing your PHP code much.
Better error handling: PDO throws exceptions, so it’s easier to catch and fix errors.
Object-oriented:
PDO uses OOP style, so your code is clean and easy to maintain.
Support for transactions: Helps you keep database operations safe and consistent.
PHP CRUD Operations using PDO5>
Create (Insert) Operation
The Create operation is used to add new records into the database table.
The Create operation is used to add new records into the database table.
MySQL table creation SQL for the users table that fits the CRUD examples
CREATE TABLE users ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL, email VARCHAR(150) NOT NULL UNIQUE );
Create ऑपरेशन का उपयोग database table में नया रिकॉर्ड डालने के लिए किया जाता है।
try {
$conn = new PDO("mysql:host=localhost;dbname=testdb", "root", "mysql db password");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO users (name, email) VALUES ('Ravi', 'ravi@example.com')";
$conn->exec($sql);
echo "New record created successfully";
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}
Output:
Read (Select) Operation
The Read operation fetches records from the database table and displays them.
Read ऑपरेशन database table से डेटा पढ़ता है और उसे display करता है।
try {
$conn = new PDO("mysql:host=localhost;dbname=testdb", "root", "ur mysql db password");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->query("SELECT id, name, email FROM users");
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo $row['id'] . ": " . $row['name'] . " - " . $row['email'] . "<br>";
}
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}
Output:
2: Anjali - anjali@example.com
3: Amit - amit@example.com
Update Operation
The Update operation modifies existing records in the database.
Update ऑपरेशन database में पहले से मौजूद रिकॉर्ड को बदलने के लिए होता है।
try {
$conn = new PDO("mysql:host=localhost;dbname=testdb", "root", "");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "UPDATE users SET email = 'ravi.new@example.com' WHERE name = 'Ravi'";
$stmt = $conn->prepare($sql);
$stmt->execute();
echo $stmt->rowCount() . " record(s) updated";
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}
Output:
Delete Operation
The Delete operation removes records from the database table.
Delete ऑपरेशन database से रिकॉर्ड हटाने के लिए होता है।
try {
$conn = new PDO("mysql:host=localhost;dbname=testdb", "root", "");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "DELETE FROM users WHERE name = 'Ravi'";
$stmt = $conn->prepare($sql);
$stmt->execute();
echo $stmt->rowCount() . " record(s) deleted";
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}