Class xử lý database trong hướng đối tượng (phần 2)

https://freetuts.net/class-xu-ly-database-trong-huong-doi-tuong-phan-2-189.html

C:\xampp\htdocs\php\DB_driver.php

<?php
class DB_driver {
  private $__conn;
  public function connect() {
    if (!$this->__conn) {
      $this->__conn = mysqli_connect('localhost', 'root', '', 'demo') or die('Lỗi kết nối');
      mysqli_query($this->__conn, "SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'");
    }
  }
  public function dis_connect() {
    if ($this->__conn) {
      mysqli_close($this->__conn);
    }
  }
  public function insert($table, $data) {
    $this->connect();
    $field_list = '';
    $value_list = '';
    foreach ($data as $key => $value) {
      $field_list .= ",$key";
      $value_list .= ",'" . mysqli_escape_string($this->__conn, $value) . "'";
    }
    $sql = 'INSERT INTO ' . $table . '(' . trim($field_list, ',') . ') VALUES (' . trim($value_list, ',') . ')';
    return mysqli_query($this->__conn, $sql);
  }
  public function update($table, $data, $where) {
    $this->connect();
    $sql = '';
    foreach ($data as $key => $value) {
      $sql .= "$key = '" . mysqli_escape_string($this->__conn,$value) . "',";
    }
    $sql = 'UPDATE ' . $table . ' SET ' . trim($sql, ',') . ' WHERE ' . $where;
    return mysqli_query($this->__conn, $sql);
  }
  public function remove($table, $where) {
    $this->connect();
    $sql = "DELETE FROM $table WHERE $where";
    return mysqli_query($this->__conn, $sql);
  }
  public function get_list($sql) {
    $this->connect();
    $result = mysqli_query($this->__conn, $sql);
    if (!$result) {
      die('Câu truy vấn bị sai');
    }
    $return = array();
    while ($row = mysqli_fetch_assoc($result)) {
      $return[] = $row;
    }
    mysqli_free_result($result);
    return $return;
  }
  public function get_row($sql) {
    $this->connect();
    $result = mysqli_query($this->__conn, $sql);
    if (!$result) {
      die('Câu truy vấn bị sai');
    }
    $row = mysqli_fetch_assoc($result);
    mysqli_free_result($result);
    if ($row) {
      return $row;
    }
    return false;
  }
}
?>

C:\xampp\htdocs\php\DB_business.php

<?php
require "DB_driver.php";
class DB_business extends DB_driver {
  protected $_table_name = '';
  protected $_key = '';
  function __construct() {
    parent::connect();
  }
  function __destruct() {
    parent::dis_connect();
  }
  function add_new($data) {
    // return parent::insert($this->_table_name, $data);
    return $this->insert($this->_table_name, $data);
  }
  function delete_by_id($id) {
    return $this->remove($this->_table_name, $this->_key . '=' . (int) $id);
  }
  function update_by_id($data, $id) {
    return $this->update($this->_table_name, $data, $this->_key . "=" . (int) $id);
  }
  function select_by_id($select, $id) {
    $sql = "SELECT $select FROM " . $this->_table_name . " WHERE " . $this->_key . " = " . (int) $id;
    return $this->get_row($sql);
  }
}
?>

C:\xampp\htdocs\php\demo.php

<?php
require 'DB_business.php';
class Customer extends DB_business {
  public function __construct() {
    $this->_table_name = 'customer';
    $this->_key = 'id';
    parent::__construct();
  }
}
$customer = new Customer();
$customer->add_new(array(
  'name'  => 'Phạm Ngọc Hoàng',
  'phone' => '0970 306 603',
));
// $customer->delete_by_id(1);
// $customer->update_by_id(array(
//   'name' => 'thehalfheart',
// ), 2);
// var_dump($customer->select_by_id('*', 2));

Last updated