Skip to main content

OOP PHP CRUD Operation Using MySQLi - Part 1

Screenshot
Body
In this tutorial, we will tackle about Object Oriented Programming PHP CRUD using MySQLi as a database server. Object Oriented Programming is an organize structured that consist of different functions inside a class. It is mostly used by an advanced programmer because for them this is some what a new challenge for their programming carreer. Let's see how OOP is done in creating a connection. OOP way
  1. class db{
  2. public $conn
  3.  
  4. public function __construct(){
  5. $this->conn->connect();
  6. }
  7.  
  8. public function connect(){
  9. $this->conn = new mysqli('localhost', 'root', '', 'yourdb');
  10. }
  11. }
Normal way
  1. mysqli_connect('localhost', 'root', '', 'yourdb');
You'll see that OOP is a little bit more complicated than the way we normally use. By the way, this tutorial is consist of two parts, the one that we will do for now is on how to create and read the data in the database. So let's start coding. Creating the database Open your database web server then create a database name in it 'crud', after that click SQL and copy/paste the code below.
  1. CREATE TABLE `member` (
  2. `mem_id` int(11) NOT NULL AUTO_INCREMENT,
  3. `firstname` varchar(30) NOT NULL,
  4. `lastname` varchar(30) NOT NULL,
  5. PRIMARY KEY (`mem_id`)
  6. ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
Creating the database connection Open your any kind of text editor(notepadd++, etc..). Then just copy/paste the code below then name it 'config.php'
  1. <?php
  2. define('db_host', 'localhost');
  3. define('db_user', 'root');
  4. define('db_pass', '');
  5. define('db_name', 'crud');
  6.  
  7. class db_connect{
  8. public $host = db_host;
  9. public $user = db_user;
  10. public $pass = db_pass;
  11. public $dbname = db_name;
  12. public $conn;
  13. public $error;
  14.  
  15. public function connect(){
  16. $this->conn = new mysqli($this->host, $this->user, $this->pass, $this->dbname);
  17. if(!$this->conn){
  18. $this->error = "Fatal Error: Can't connect to database" . $this->connect->connect_error();
  19. return false;
  20. }
  21. }
  22. }
  23. ?>
The code above will create the database connection script. The CRUD(Create and Read) functions After creating the database connect, we will now create the CRUD function inside the class, to do that just copy/paste the code below then name it 'class.php'
  1. <?php
  2. require 'config.php';
  3.  
  4. class db_class extends db_connect{
  5.  
  6. public function __construct(){
  7. $this->connect();
  8. }
  9.  
  10. public function create($firstname, $lastname){
  11. $stmt = $this->conn->prepare("INSERT INTO `member` (`firstname`, `lastname`) VALUES (?, ?)") or die($this->conn->error);
  12. $stmt->bind_param("ss", $firstname, $lastname);
  13. if($stmt->execute()){
  14. $stmt->close();
  15. $this->conn->close();
  16. return true;
  17. }
  18. }
  19.  
  20. public function read(){
  21. $stmt = $this->conn->prepare("SELECT * FROM `member` ORDER BY `lastname` ASC") or die($this->conn->error);
  22. if($stmt->execute()){
  23. $result = $stmt->get_result();
  24. return $result;
  25. }
  26. }
  27. }
  28. ?>
The code above will generate the request after the function is called, The create() function will store the value into the database, while the read() function will retrieve the data within the database and display it in the webpage The Mark-up Form This is the form we will use in the CRUD functions, copy/paste the code below and name it 'index.php'
  1. <!DOCTYPE html>
  2. <html lang = "en">
  3. <head>
  4. <meta charset = "UTF-8" name = "viewport" content = "width-device=width, initial-scale=1" />
  5. <link rel = "stylesheet" type = "text/css" href = "css/bootstrap.css" />
  6. <title>OOP PHP CRUD Operation</title>
  7. </head>
  8. <body>
  9. <nav class = "navbar navbar-default">
  10. <div class = "container-fluid">
  11. <a class = "navbar-brand" href = "https://www.sourcecodester.com">Sourcecodester</a>
  12. </div>
  13. </nav>
  14. <div class = "row">
  15. <div class = "col-md-3">
  16. </div>
  17. <div class = "col-md-6 well">
  18. <h3 class = "text-primary">OOP PHP CRUD Operation Using MySQLi - Part 1</h3>
  19. <hr style = "border-top:1px dotted #000;"/>
  20. <form method = "POST" class = "form-inline" action = "create.php">
  21. <div class = "form-group">
  22. <label>Firstname:</label>
  23. <input type = "text" name = "firstname" class = "form-control" required = "required"/>
  24. </div>
  25. <div class = "form-group">
  26. <label>Lastname:</label>
  27. <input type = "text" name = "lastname" class = "form-control" required = "required"/>
  28. </div>
  29. <div class = "form-group">
  30. <button name = "save" class = "btn btn-primary"><span class = "glyphicon glyphicon-plus"></span> Add</button>
  31. </div>
  32. </form>
  33. <br />
  34. <table class = "table table-bordered alert-warning table-hover">
  35. <thead>
  36. <th>Firstname</th>
  37. <th>Lastname</th>
  38. <th>Action</th>
  39. </thead>
  40. <tbody>
  41. <?php
  42. require 'class.php';
  43. $conn = new db_class();
  44. $read = $conn->read();
  45. while($fetch = $read->fetch_array()){
  46. ?>
  47. <tr>
  48. <td><?php echo $fetch['firstname']?></td>
  49. <td><?php echo $fetch['lastname']?></td>
  50. <td><center><a class = "btn btn-warning"><span class = "glyphicon glyphicon-edit"></span> Update</a> | <a class = "btn btn-danger"><span class = "glyphicon glyphicon-trash"></span> Delete</a></center></td>
  51. </tr>
  52. <?php
  53. }
  54. ?>
  55. </tbody>
  56. </table>
  57. </div>
  58. </div>
  59. </body>
  60. </html>
In this form you'll notice that we used read() inside the table, the function read() here call back the requested function within the class.php to display it in an array of data The Save query After creating the form we will now create the submit query, copy/paste the code below then name it 'create.php'
  1. <?php
  2. require_once 'class.php';
  3.  
  4. if(ISSET($_POST['save'])){
  5. $firstname = $_POST['firstname'];
  6. $lastname = $_POST['lastname'];
  7. $conn = new db_class();
  8. $conn->create($firstname, $lastname);
  9. header('location: index.php');
  10. }
  11.  
  12. ?>
The code above stored the all the value within the form and call the function create() to put all the stored data into database. There you have it, we have created a form on how to create and read the data in the database in OOP way. I hope that this tutorial help you. On my next tutorial, we will tackle on how to update and delete a data based on the code I have given. Just stay tuned in this site. Enjoy Coding!!

Tags

Comments

Submitted byelen (not verified)on Sun, 08/13/2017 - 18:15

hello there, it would be great if you kindly post some OOPhp learning tacts based on your programming experienced. Thanks.
Submitted bymohd miraj (not verified)on Sun, 07/15/2018 - 11:14

Wo bro good job very easy to understand language
Submitted byrazormiston Thu, 07/26/2018 - 16:21

In reply to by mohd miraj (not verified)

I'm glad you like it, I made these simpler so that beginners can understand this OOP thing in an easy way.

Add new comment