Showing posts with label Mysqli CRUD Operations. Show all posts
Showing posts with label Mysqli CRUD Operations. 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

CRUD operations in PHP with MySQLi Object Oriented Interface

My previous article CRUD operations with MySQLi procedural functions is demonstrating about performing CRUD operations on MySQL database. As we know that one of the best feature of MySQLi is that it also provides an object oriented interface, which is the most preferred approach these days for application development. So this article provides an Object Oriented version of the previous article to demonstrate how to use MySQLi object oriented interface for performing CRUD operations.


1. Create Connection

  1. <?php
  2. // Create connection
  3. $con = @new mysqli($host_name, $username, $password, $database);
  4. //check for connection errors
  5. if ($con->connect_errno) {
  6. die('Connect Error: ' . $con->connect_error);
  7. }
  8. ?>

2. Create Table

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

3. INSERT Operation

Insert one row into users table.

  1. <?php
  2. //--------SQL 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 (!$con->query($query)) {
  5. echo "Error in inserting records : ". $con->error;
  6. } else {
  7. echo "New record added successfully";
  8. }
  9. ?>
 Output : New record added successfully

4. SELECT Operation

Select all records from the users table.

  1. <?php
  2. //------SQL statement-----------
  3. $query = "SELECT * FROM users";
  4. //--------execute query------------
  5. $result = $con->query($query);
  6. if ($result->num_rows > 0) {
  7. //print data of each row
  8. while($row = $result->fetch_assoc()) {
  9. echo "id: " . $row["id"]."<br>";
  10. echo "first_name: " . $row["first_name"]."<br>";
  11. echo "last_name: " . $row["last_name"]."<br>";
  12. echo "email: " . $row["email"]."<br>";
  13. echo "city: " . $row["city"];
  14. }
  15. } else {
  16. echo "No record exists";
  17. }
  18. ?>
Output :
id: 1
first_name: Amit
last_name: Verma
email: amit.verma21@gmail.com
city: Bhopal

5. UPDATE Operation

Update the city column for record with id = 1.

  1. <?php
  2. //---------SQL statement-------------
  3. $query = "UPDATE users SET city='Pune' WHERE id=1";
  4. //-----execute query------
  5. if (!$con->query($query)) {
  6. echo "Error : ". $con->error;
  7. } else {
  8. echo "Record updated successfully";
  9. }
  10. echo "<br>";
  11. //-------fetch records from table---------
  12. $query2 = "SELECT city FROM users";
  13. $result = $con->query($query2);
  14. if ($result->num_rows > 0) {
  15. //print data of each row
  16. while($row = $result->fetch_assoc()) {
  17. echo "city : ". $row["city"];
  18. }
  19. } else {
  20. echo "No record exists";
  21. }
  22. ?>
Output :
Record updated successfully
city : Pune

6. DELETE Operation

Delete record from table whose id  = 1.

  1. <?php
  2. //---------SQL statement-------------
  3. $query = "DELETE FROM users WHERE id=1";
  4. //-----execute query------
  5. if (!$con->query($query)) {
  6. echo "Error : ". $con->error;
  7. } else {
  8. echo "Record deleted successfully";
  9. }
  10. echo "<br><br/>";
  11. //-------fetch records from table---------
  12. $query2 = "SELECT * FROM users";
  13. $result = $con->query($query2);
  14. if ($result->num_rows > 0) {
  15. //print data of each row
  16. while($row = $result->fetch_assoc()) {
  17. echo "city: ". $row["city"];
  18. }
  19. } else {
  20. echo "No record exists";
  21. }
  22. ?>
Output :
Record deleted successfully

No record exists