DEV Community

Cover image for Introduction to CRUD Functions with Procedural PHP...
Excel Prosper
Excel Prosper

Posted on

Introduction to CRUD Functions with Procedural PHP...

CRUD stands for Create, Read, Update, and Delete, and these are the four basic functions of persistent storage, such as databases. In the context of PHP, CRUD typically refers to the creation and management of websites or web applications that can create, read, update, and delete data stored in a database.

Procedural PHP is a programming style in which functions and code are organized into procedures, or blocks of code that perform a specific task. It is a simpler and more traditional style of programming compared to object-oriented programming, which organizes code into objects that can interact with one another.

Here is an introduction to CRUD functions using procedural PHP:

Create

To create a new record in a database using PHP, you can use the **INSERT **statement. This statement takes a set of values and inserts them into a table in the database. For example:

$query = "INSERT INTO users (name, email) VALUES ('John', 'john@example.com')";
mysqli_query($connection, $query);

Enter fullscreen mode Exit fullscreen mode

This will insert a new row into the users table with the values 'John' and 'john@example.com' for the name and email columns, respectively.

Read

To read data from a database using PHP, you can use the **SELECT **statement. This statement retrieves data from a table in the database and returns it as a result set. For example:

$query = "SELECT * FROM users WHERE name='John'";
$result = mysqli_query($connection, $query);
while ($row = mysqli_fetch_assoc($result)) {
  echo $row['name'] . ': ' . $row['email'] . '<br>';
}

Enter fullscreen mode Exit fullscreen mode

This will select all rows from the users table where the name column is 'John', and then it will loop through the result set and print out the name and email values for each row.

Update

To update a record in a database using PHP, you can use the **UPDATE **statement. This statement modifies the values of a set of existing rows in a table. For example:

$query = "UPDATE users SET name='Jane' WHERE email='john@example.com'";
mysqli_query($connection, $query);

Enter fullscreen mode Exit fullscreen mode

This will update all rows in the users table where the email column is 'john@example.com' and set the name column to 'Jane'.

Delete

To delete a record from a database using PHP, you can use the **DELETE **statement. This statement removes a set of rows from a table. For example:

$query = "DELETE FROM users WHERE email='john@example.com'";
mysqli_query($connection, $query);

Enter fullscreen mode Exit fullscreen mode

This will delete all rows from the users table where the email column is 'john@example.com'.

Conclusion:
This is just a brief introduction to CRUD **functions using procedural **PHP.

There are many other details and considerations to keep in mind when working with databases in PHP, such as security, performance, and error handling.

You will be able to begin working with databases in your future PHP projects once you have learned these basic coding techniques.

Top comments (0)