Showing posts with label Mysqli Procedural Functions. Show all posts
Showing posts with label Mysqli Procedural Functions. Show all posts

Monday, 24 September 2018

CRUD operations with MySQLi procedural functions

In previous posts we have learned about creating a connection to a MySQL database with procedural and object oriented way, we also learn how to run a query with mysqli. 

This article demonstrates how to perform different CRUD operations with MySQLi procedural functions.

CRUD

CRUD represents the four basic operations of the database.
C : C stands for Create (INSERT), which defines creating or inserting new data into the database.
R : R stands for Read (SELECT), which defines reading or retrieving records from database.
U : U stands for Update (UPDATE), update selected records of database.
D : D stands for Delete (DELETE), delete or remove the selected records from database.

For performing above operations first we have to create a connection to MySQL database and have to create a table to perform the above operations on it.

Create Connection

  1. <?php
  2. //create connection
  3. $con = @mysqli_connect($host_name, $username, $password, $database);
  4. // check connection errors
  5. if (!$con) {
  6. die('Connect Error: ' . mysqli_connect_errno());
  7. }
  8. ?>

Create Table

Below is the code to create a table users.
  1. <?php
  2. $query = "CREATE TABLE users
  3. (
  4. id int AUTO_INCREMENT PRIMARY KEY,
  5. first_name varchar(80),
  6. last_name varchar(80),
  7. email varchar(150),
  8. city varchar(50)
  9. )";
  10. //------execute query--------------
  11. if (mysqli_query($con, $query)) {
  12. echo "Table created successfully";
  13. } else {
  14. echo "Error : ".mysqli_error($con);
  15. }
  16. ?>
Output : Table created successfully

INSERT

INSERT statement is used for inserting new records into a MySQL table.

Syntax : INSERT INTO `table_name` (Field1, Field2, Field3,.....) VALUES (Value1, Value2, Value3,.....)

Example : Let insert one row into users table.

  1. <?php
  2. //--------Insert statement---------
  3. $query = "INSERT INTO users (id, first_name, last_name, email, city) VALUES (1, 'Rohit', 'Kumar', 'rohit.kumar25@gmail.com', 'New Delhi')";
  4. if (mysqli_query($con, $query)) {
  5. echo "New record added successfully";
  6. } else {
  7. echo "Error: ". mysqli_error($con);
  8. }
  9. ?>
Output : New record added successfully

SELECT

The SELECT statement is used for retrieving records from MySQL table.

Syntax : SELECT column_name(one or more) FROM table_name

Note :  To fetch all records use * instead of column names.
Example : Retrieve all records from the users table.

  1. <?php
  2. $query = "SELECT * FROM users";
  3. $result = mysqli_query($con, $query);
  4. if (mysqli_num_rows($result) > 0) {
  5. //print data of each row
  6. while($row = mysqli_fetch_assoc($result)) {
  7. echo "id: " . $row["id"]."<br>";
  8. echo "first_name: " . $row["first_name"]."<br>";
  9. echo "last_name: " . $row["last_name"]."<br>";
  10. echo "email: " . $row["email"]."<br>";
  11. echo "city: " . $row["city"];
  12. }
  13. } else {
  14. echo "No record exists";
  15. }
  16. ?>
Output :
id: 1
first_name: Rohit
last_name: Kumar
email: rohit.kumar25@gmail.com
city: New Delhi

UPDATE

UPDATE statement is used to update the existing record of the table.

Syntax : UPDATE table_name SET column1=value1, column2=value2,... WHERE condition_column = condition_value

Example :

  1. <?php
  2. $query = "UPDATE users SET city='Mumbai' WHERE id=1";
  3. if (mysqli_query($con, $query)) {
  4. echo "Record updated successfully";
  5. } else {
  6. echo "Error: " . mysqli_error($con);
  7. }
  8. echo "<br>";
  9. $query = "SELECT city FROM users WHERE id = 1";
  10. $result = mysqli_query($con, $query);
  11. if (mysqli_num_rows($result) > 0) {
  12. //print data of each row
  13. while($row = mysqli_fetch_assoc($result)) {
  14. echo "city: " . $row["city"];
  15. }
  16. } else {
  17. echo "No record exists";
  18. }
  19. ?>
Output :
Record updated successfully

city: Mumbai

DELETE

DELETE statement is used for deleting records from a table.

Syntax : DELETE FROM table_name WHERE column_name = value

Example : Delete user record with id = 1.

  1. <?php
  2. $query = "DELETE FROM users WHERE id=1";
  3. if (mysqli_query($con, $query)) {
  4. echo "Record deleted successfully";
  5. } else {
  6. echo "Error : " . mysqli_error($con);
  7. }
  8. echo "<br/><br/>";
  9. $query = "SELECT * FROM users";
  10. $result = mysqli_query($con, $query);
  11. if (mysqli_num_rows($result) > 0) {
  12. //print data of each row
  13. while($row = mysqli_fetch_assoc($result)) {
  14. echo "city: " . $row["city"];
  15. }
  16. } else {
  17. echo "No record exists";
  18. }
  19. ?>
Output :
Record deleted successfully

No record exists