PDO Database Connection:
<?php
try {
$pdo = new PDO("mysql:host=localhost;dbname=your_database_name", "your_username", "your_password");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
} catch (PDOException $e) {
die("Connection failed: " . $e->getMessage());
}
?>
CRUD Operations with PDO:
Create (INSERT):
try {
$sql = "INSERT INTO your_table (column1, column2, column3) VALUES (:value1, :value2, :value3)";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':value1', $value1);
$stmt->bindParam(':value2', $value2);
$stmt->bindParam(':value3', $value3);
$value1 = "Some Value";
$value2 = "Another Value";
$value3 = 123;
$stmt->execute();
echo "Record inserted successfully.";
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}
Read (SELECT) WIthout Loop:
try {
$sql = "SELECT * FROM users WHERE user_email = :email AND user_password = :password";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':email', $l_email);
$stmt->bindParam(':password', $encrypted_password);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if ($row) {
$user_id = $row['id'];
if ($user_id != '') {
$message = 'Logged in Successfully';
} else {
$message = 'Invalid email or password';
}
} else {
$message = 'Invalid email or password';
}
echo $message;
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}
Update (UPDATE):
try {
$sql = "UPDATE your_table SET column1 = :value1, column2 = :value2 WHERE id = :id";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':value1', $value1);
$stmt->bindParam(':value2', $value2);
$stmt->bindParam(':id', $id);
$value1 = "Updated Value";
$value2 = "New Value";
$id = 1; // Example record ID
$stmt->execute();
echo "Record updated successfully.";
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}
Delete (DELETE):
try {
$sql = "DELETE FROM your_table WHERE id = :id";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':id', $id);
$id = 1; // Example record ID to delete
$stmt->execute();
echo "Record deleted successfully.";
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}
Read (Select) With Loop
try {
$sql = "SELECT * FROM users WHERE user_email = '$l_email' AND user_password = '$encrypted_password'";
$stmt = $pdo->query($sql);
$stmt->execute();
while ($row = $stmt->fetch()) {
// Process the retrieved data
$value = $row['id'];
echo "$new_value";
}
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}
}
Explanation:
- The PDO database connection code establishes a connection to your MySQL database, and it sets the error mode and fetch mode to provide useful error messages and fetch data as associative arrays.
- The CRUD examples demonstrate how to perform basic database operations with PDO: Create (INSERT), Read (SELECT), Update (UPDATE), and Delete (DELETE).
- Prepared statements are used to prevent SQL injection by binding values to placeholders.
Make sure to replace placeholders like your_database_name
, your_username
, your_password
, and your_table
with your actual database details and table name. Also, modify the column names and values accordingly for your specific use case.