Description
A developer's journey through code. I build, I break, and I write about it. Explore articles on modern software development, programming tips, and more.
In the field of web development, managing database records efficiently is very important. PHP is a versatile server-side scripting language that offers various methods to interact with databases. One powerful tool in the PHP developer's arsenal is PDO (PHP Data Objects), which provides a consistent interface for accessing databases, including MySQL. In this article, I will walk you through into implementing CRUD operations (Create, Read, Update, Delete) using PDO to manipulate MySQL database table records effectively like a pro.
PDO is a database access layer providing a uniform method of access to multiple databases. It supports various database drivers, including MySQL, PostgreSQL, SQLite, and more. PDO offers several advantages over traditional database access methods like MySQLi, such as prepared statements, which help prevent SQL injection attacks and enhance security.
Before you get started with PDO CRUD operations, ensure you have the following prerequisites:
First, let us create a MySQL database and a table to perform CRUD operations. Execute the following SQL commands in your MySQL client or phpMyAdmin:
CREATE DATABASE IF NOT EXISTS `pdo_crud`;
USE `pdo_crud`;
CREATE TABLE IF NOT EXISTS `users` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`name` VARCHAR(100) NOT NULL,
`email` VARCHAR(100) NOT NULL
);
This SQL script creates a database named 'pdo_crud' and a table named 'users' with columns for id, name, and email.
Next, let us establish a connection to the MySQL database using PDO in PHP. Create a new PHP file (e.g., db_connection.php) and add the following code:
$host = 'localhost';
$dbname = 'pdo_crud';
$username = 'your_username';
$password = 'your_password';
try {
$pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
} catch (PDOException $e) {
die("Connection failed: " . $e->getMessage());
}
Replace 'your_username' and 'your_password' with your MySQL username and password, respectively. This script establishes a connection to the 'pdo_crud' database.
Now, let us implement CRUD operations using PDO:
$name = 'John Doe';
$email = 'john@example.com';
$stmt = $pdo->prepare("INSERT INTO users (name, email) VALUES (:name, :email)");
$stmt->bindParam(':name', $name);
$stmt->bindParam(':email', $email);
$stmt->execute();
echo "Record inserted successfully";
include 'db_connection.php';
$stmt = $pdo->query("SELECT * FROM users");
while ($row = $stmt->fetch()) {
echo "ID: " . $row['id'] . ", Name: " . $row['name'] . ", Email: " . $row['email'] . "<br/>";
}
include 'db_connection.php';
$id = 1;
$newName = 'Jane Doe';
$stmt = $pdo->prepare("UPDATE users SET name = :name WHERE id = :id");
$stmt->bindParam(':name', $newName);
$stmt->bindParam(':id', $id);
$stmt->execute();
echo "Record updated successfully";
include 'db_connection.php';
$id = 1;
$stmt = $pdo->prepare("DELETE FROM users WHERE id = :id");
$stmt->bindParam(':id', $id);
$stmt->execute();
echo "Record deleted successfully";
In this article, we explored how to implement CRUD operations using PHP PDO to manipulate MySQL database table records. PDO offers a secure, efficient, and consistent way to interact with databases, making it a preferred choice for database operations in PHP applications. By following these steps I have provided, you can enhance your web development skills and build robust database-driven applications with PHP and MySQL.
Cookies improve user experience on SunshineIHCTS. By continuing to use this website, you consent to the use of cookies in accordance with the Privacy policy.
A developer's journey through code. I build, I break, and I write about it. Explore articles on modern software development, programming tips, and more.
Comments section
You need to be logged in to comment, Login or Register.Approved comments:
No comments yet! be the first to comment