Advertisement
gsmashik

DB Class

Aug 18th, 2018
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.41 KB | None | 0 0
  1.  <?php
  2. class Connection
  3. {
  4.     public $dbhost = "localhost";
  5.     public $dbdriver = "mysql";
  6.     public $dbuser = "root";
  7.     public $dbpass = "usbw";
  8.     public $dbname = "demo";
  9.     public $charset = "utf8mb4";
  10.     public $dbport = "3306";
  11.     public $pdo;
  12.     public $notification;
  13.     public function __construct()
  14.     {
  15.         if (!isset($this->pdo)) {
  16.             try {
  17.                 $this->pdo = new PDO($this->dbdriver . ":host=" . $this->dbhost . ";port=" . $this->dbport . ";dbname=" . $this->dbname . ";" . $this->charset, $this->dbuser, $this->dbpass, array(
  18.                     PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  19.                     PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  20.                     PDO::ATTR_EMULATE_PREPARES => false,
  21.                     PDO::ATTR_PERSISTENT => true
  22.                 ));
  23.                 if (!empty($this->pdo)) {
  24.                     $this->notification = "Connected";
  25.                 }
  26.             }
  27.             catch (PDOException $e) {
  28.                 echo $e->getMessage();
  29.             }
  30.         }
  31.     }
  32.  
  33.  
  34.     public function insert($table,$data){
  35.  
  36.         if (!empty($table) && !empty($data) && is_array($data)) {
  37.             $column = "";
  38.             $value = "";
  39.             $i = 0;
  40.             if (!array_key_exists("created", $data)) {
  41.                
  42.                 $data['created'] = date("Y-m-d H-i-s");
  43.             }
  44.  
  45.             if (!array_key_exists("modified", $data)) {
  46.                 $data['modified'] = date("Y-m-d H-i-s");
  47.             }
  48.  
  49.             foreach ($data as $key => $value) {
  50.                 $pre = ($i > 0)?",":"";
  51.                 $column = $pre.$key;
  52.                 $value = $pre."'".$value."'";
  53.                 $i++;
  54.             }
  55.  
  56.             $query = "INSERT INTO ".$table." (".$column.") VALUES (".$value.")";
  57.             $insert = $this->pdo->query($query);
  58.             return $insert?$this->pdo->insert_id:false;
  59.         }else{
  60.             return false;
  61.         }
  62.     }
  63.  
  64. SELECT column1, column2 FROM table_name WHERE condition1 AND condition2 AND condition3   ORDER BY Country ASC, CustomerName DESC LIMIT 0, 10  OFFSET 2;
  65. public function get_row ($table,$condition = array()){
  66.  
  67. $sql = " SELECT ";
  68. $sql .= array_key_exists("select", $condition)?$condition["select"]:" * ";
  69. $sql .= " FROM ".$table;
  70. if (array_key_exists("where", $condition)) {
  71.     $sql . = " WHERE ";
  72.     $i =0;
  73. foreach ($condition['where'] as $key => $value) {
  74.    
  75.     $pre = ($i > 0 )?" AND ":"";
  76.     $sql .=$pre.$key." = '".$value."'";
  77.     $i++;
  78. }
  79.  
  80. }
  81.  
  82. if (array_key_exists("order_by", $condition)) {
  83.     $sql .= " OREDR BY ".$condition['id'];
  84. }
  85.  
  86. if (array_key_exists("start", $condition) && array_key_exists("limit", $condition) && array_key_exists("offset", $condition)) {
  87.    
  88.     $sql .="  LIMIT ".$condition['start'].",".$condition['limit']." OFFSET ".$condition['offset'];
  89. }
  90. elseif (!array_key_exists("start", $condition) && array_key_exists("offset", $condition)) {
  91.         $sql .="  LIMIT ".$condition['limit']." OFFSET ".$condition['offset'];
  92.  
  93. }
  94. elseif (!array_key_exists("start", $condition) && !array_key_exists("offset", $condition) && array_key_exists("limit", $condition)) {
  95.         $sql .="  LIMIT ".$condition['limit'];
  96.  
  97. }
  98. // select
  99. $result  = $this->pdo->query($sql);
  100.  
  101. if (array_key_exists("return_type", $condition) && $condition['return_type'] != "all") {
  102.     switch ($condition['return_type']) {
  103.         case "count":
  104.             $data = $result->num_rows;
  105.             break;
  106.         case "single":
  107.             $data = $result->fetch_assoc();
  108.             break;     
  109.         default:
  110.             $data = "";
  111.            
  112.     }
  113. }
  114.  
  115. else {
  116.     if ($result->num_rows) > 0 {
  117.         while ($row = $result->fetch_assoc()) {
  118.             $data[]=$row;
  119.         }
  120.     }
  121. }
  122. return !empty($data)?$data:false;
  123. }
  124. $data = [
  125.     'name' => $name,
  126.     'surname' => $surname,
  127.     'sex' => $sex,
  128. ];
  129. $stmt = $this->pdo->prepare ("INSERT INTO user (firstname, surname) VALUES (:f-name, :s-name)");
  130. $stmt -> bindParam(':f-name', 'John');
  131. $stmt -> bindParam(':s-name', 'Smith');
  132. $stmt -> execute();
  133. public function insert ($table,$data){
  134. if (!empty($data) && is_array($data)) {
  135.    
  136.     $columns = "";
  137.     $values = "";
  138.     $i = 0;
  139.     foreach ($data as $key => $value) {
  140.         $pre = ($i > 0)?",":"";
  141.         $columns =$pre.$key;
  142.         $values = $pre."'".":".$value."'";
  143.         $i++;
  144.  
  145.     }
  146.     $query = " INSERT INTO ".$table." (".$columns.") VALUES (".$values.")";
  147.     $stmt  = $this->pdo->prepare($query);
  148.     $bindcolumn = "";
  149.     $bindvalue = "";
  150.     foreach ($data as $keyb => $valueb) {
  151.         $stmt ->bindParam (':'.$keyb, $valueb);
  152.     }
  153.                 return $stmt -> execute();
  154. }
  155. else {
  156.     return false;
  157. }
  158. }
  159.  
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement