Monday 24 September 2018

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

0 comments:

Post a Comment