How do I perform CRUD operations

CRUD operations stand for Create, Read, Update, and Delete. These operations are fundamental for interacting with data in a web application. Below is a brief explanation and example for each operation.

Create

The Create operation is used to add new data to the database.

Read

The Read operation retrieves data from the database.

Update

The Update operation modifies existing data in the database.

Delete

The Delete operation removes data from the database.

Example:

<?php // Establish database connection $conn = new mysqli('localhost', 'username', 'password', 'database'); // Create $sql_create = "INSERT INTO users (name, email) VALUES ('John Doe', 'john@example.com')"; $conn->query($sql_create); // Read $result = $conn->query("SELECT * FROM users"); while ($row = $result->fetch_assoc()) { echo $row['name'] . ' - ' . $row['email'] . '<br>'; } // Update $sql_update = "UPDATE users SET email='john.doe@example.com' WHERE name='John Doe'"; $conn->query($sql_update); // Delete $sql_delete = "DELETE FROM users WHERE name='John Doe'"; $conn->query($sql_delete); ?>

CRUD Create Read Update Delete PHP MySQL