Certainly! CRUD stands for Create, Read, Update, and Delete, which are the basic operations you perform on a database. Using PHP’s PDO (PHP Data Objects) with an Object-Oriented Programming (OOP) approach is a good practice for database operations. Below is an example of a simple PHP CRUD application using PDO and OOP:

  1. Create a Database Table: Let’s assume you have a table named users with columns id, username, and email.
   CREATE TABLE users (
       id INT PRIMARY KEY AUTO_INCREMENT,
       username VARCHAR(255) NOT NULL,
       email VARCHAR(255) NOT NULL
   );
  1. Create a PDO Connection Class:
   <?php
   class Database {
       private $host = 'your_host';
       private $user = 'your_username';
       private $password = 'your_password';
       private $dbname = 'your_database';
       private $conn;

       public function __construct() {
           $dsn = "mysql:host={$this->host};dbname={$this->dbname}";

           try {
               $this->conn = new PDO($dsn, $this->user, $this->password);
               $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
           } catch (PDOException $e) {
               echo "Connection failed: " . $e->getMessage();
           }
       }

       public function getConnection() {
           return $this->conn;
       }
   }
  1. Create a User Class for CRUD Operations:
   <?php
   class User {
       private $conn;

       public function __construct($conn) {
           $this->conn = $conn;
       }

       public function create($username, $email) {
           $stmt = $this->conn->prepare("INSERT INTO users (username, email) VALUES (:username, :email)");
           $stmt->bindParam(':username', $username);
           $stmt->bindParam(':email', $email);

           return $stmt->execute();
       }

       public function read() {
           $stmt = $this->conn->query("SELECT * FROM users");
           return $stmt->fetchAll(PDO::FETCH_ASSOC);
       }

       public function update($id, $username, $email) {
           $stmt = $this->conn->prepare("UPDATE users SET username = :username, email = :email WHERE id = :id");
           $stmt->bindParam(':id', $id);
           $stmt->bindParam(':username', $username);
           $stmt->bindParam(':email', $email);

           return $stmt->execute();
       }

       public function delete($id) {
           $stmt = $this->conn->prepare("DELETE FROM users WHERE id = :id");
           $stmt->bindParam(':id', $id);

           return $stmt->execute();
       }
   }
  1. Usage:
   <?php
   // Include the classes
   require_once 'Database.php';
   require_once 'User.php';

   // Create a database connection
   $db = new Database();
   $conn = $db->getConnection();

   // Create a user object
   $user = new User($conn);

   // Example of CRUD operations
   // Create
   $user->create('john_doe', 'john@example.com');

   // Read
   $users = $user->read();
   print_r($users);

   // Update
   $user->update(1, 'new_username', 'new_email@example.com');

   // Read again to see the changes
   $users = $user->read();
   print_r($users);

   // Delete
   $user->delete(1);

   // Read again to see the changes
   $users = $user->read();
   print_r($users);

Remember to replace the database connection details ($host, $user, $password, $dbname) with your actual database credentials. This is a basic example, and in a production environment, you should handle errors more gracefully and consider security measures such as prepared statements to prevent SQL injection.

By admin