TechieClues TechieClues
Updated date Jun 23, 2023
This article provides a comprehensive guide on performing CRUD operations (Create, Read, Update, Delete) in PHP. It covers the basic syntax and usage of PHP's mysqli_query() function to execute SQL statements for interacting with a database. The article includes code examples for creating, reading, updating, and deleting data in a MySQL database using PHP.

Introduction

CRUD operations refer to Create, Read, Update, and Delete, which are the four basic functions used to manage data in a database. CRUD operations are commonly used in web applications to manipulate data stored in a database. PHP is a popular programming language used to develop web applications. In this article, we will explore how to perform CRUD operations in PHP.

Prerequisites

Before we move into the code, there are a few prerequisites that we need to cover.

  • A web server with PHP installed - Check here for installation 
  • A database management system (DBMS) such as MySQL, PostgreSQL, or SQLite
  • A PHP IDE or text editor
  • Basic knowledge of PHP and SQL

Create Operation

The create operation is used to insert new data into a database. In PHP, we can use the mysqli_query() function to execute SQL INSERT statements. Here is an example of how to create a new record in a MySQL database:

<?php
// establish database connection
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mydatabase";

$conn = mysqli_connect($servername, $username, $password, $dbname);

// check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

// prepare SQL statement
$sql = "INSERT INTO users (name, email, phone) VALUES ('John Doe', '[email protected]', '123-456-7890')";

// execute SQL statement
if (mysqli_query($conn, $sql)) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

// close database connection
mysqli_close($conn);
?>

Read Operation

The read operation is used to retrieve data from a database. In PHP, we can use the mysqli_query() function to execute SQL SELECT statements. Here is an example of how to retrieve all records from a MySQL database:

<?php
// establish database connection
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mydatabase";

$conn = mysqli_connect($servername, $username, $password, $dbname);

// check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

// prepare SQL statement
$sql = "SELECT * FROM users";

// execute SQL statement
$result = mysqli_query($conn, $sql);

// check if any rows were returned
if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while ($row = mysqli_fetch_assoc($result)) {
        echo "Name: " . $row["name"] . " - Email: " . $row["email"] . " - Phone: " . $row["phone"] . "<br>";
    }
} else {
    echo "0 results";
}

// close database connection
mysqli_close($conn);
?>

Update Operation

The update operation is used to modify existing data in a database. In PHP, we can use the mysqli_query() function to execute SQL UPDATE statements. Here is an example of how to update a record in a MySQL database:

<?php
// establish database connection
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mydatabase";

$conn = mysqli_connect($servername, $username, $password, $dbname);

// check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

// prepare SQL statement
$sql = "UPDATE users SET phone='555-555-5555' WHERE name='John Doe'";

// execute SQL statement
if (mysqli_query($conn, $sql)) {
    echo "Record updated successfully";
} else {
echo "Error updating record: " . mysqli_error($conn);
}

// close database connection
mysqli_close($conn);
?>

Delete Operation

The delete operation is used to remove data from a database. In PHP, we can use the mysqli_query() function to execute SQL DELETE statements. Here is an example of how to delete a record from a MySQL database:

<?php
// establish database connection
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mydatabase";

$conn = mysqli_connect($servername, $username, $password, $dbname);

// check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

// prepare SQL statement
$sql = "DELETE FROM users WHERE name='John Doe'";

// execute SQL statement
if (mysqli_query($conn, $sql)) {
    echo "Record deleted successfully";
} else {
    echo "Error deleting record: " . mysqli_error($conn);
}

// close database connection
mysqli_close($conn);
?>

Conclusion:

In this article, we have explored how to perform CRUD operations in PHP. We have covered the basic syntax and usage of the mysqli_query() function for executing SQL statements. By using these CRUD operations, we can easily manipulate data in a database from a PHP web application.

ABOUT THE AUTHOR

TechieClues
TechieClues

I specialize in creating and sharing insightful content encompassing various programming languages and technologies. My expertise extends to Python, PHP, Java, ... For more detailed information, please check out the user profile

https://www.techieclues.com/profile/techieclues

Comments (0)

There are no comments. Be the first to comment!!!