uwa209

classSQLPDO

Sep 23rd, 2020 (edited)
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.33 KB | None | 0 0
  1. <?php
  2.  
  3.     class SQL {
  4.         protected $con;
  5.         private $server;
  6.         private $host = 'localhost';
  7.         private $user = 'root';
  8.         private $pass = '';
  9.         private $debe = 'absen';
  10.         public $success = null;
  11.         public $message = null;
  12.         var $attribute = array(
  13.             PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  14.             PDO::ATTR_CASE => PDO::CASE_NATURAL,
  15.         );
  16.  
  17.         function __construct($h = null, $u = null, $p = null, $d = null) {
  18.             if($h == null) {
  19.                 $this->server = 'mysql:host='.$this->host.';dbname='.$this->debe;
  20.             } else {
  21.                 $this->server = 'mysql:host='.$h.';dbname='.$d;
  22.                 $this->user = $u;
  23.                 $this->pass = $p;
  24.             }
  25.            
  26.             try {
  27.                 $this->con = new PDO($this->server, $this->user, $this->pass, $this->attribute);
  28.                 $this->success = true;
  29.             }
  30.             catch (PDOException $e) {
  31.                 $this->success = false;
  32.                 $this->message = $e->getMessage();
  33.             }
  34.         }
  35.  
  36.         public function dbstatus() {
  37.             if($this->con == null) {
  38.                 return $this->message;
  39.             }
  40.         }
  41.  
  42.         public function response() {
  43.             if($this->success == false) {
  44.                 return $this->message;
  45.             } else {
  46.                 return 1;
  47.             }
  48.         }
  49.  
  50.         public function close() {
  51.             $this->con = null;
  52.         }
  53.  
  54.         public function insertid() {
  55.             return $this->con->lastInsertId();
  56.         }
  57.         /*
  58.         if any string with `field` will be replace with ``
  59.         */
  60.         public function insertUPLOAD($table ,$data)
  61.         {
  62.             try {
  63.                 $field = $value = '';
  64.                 foreach($data as $f=>$v) {
  65.                     $field .= ',`'.str_replace(':','',str_replace("field","",$v)).'`';
  66.                     $value .= ','.$v;
  67.                 }
  68.                 $field = substr($field,1);
  69.                 $value = substr($value,1);
  70.                 $query = 'INSERT INTO '.$table.' ('.$field.') VALUES ('.$value.')';
  71.                 $st = $this->con->prepare($query);
  72.                 $st->execute($data);
  73.                 $this->success = true;
  74.             }
  75.             catch (PDOException $e) {
  76.                 $this->success = false;
  77.                 $this->message = $e->getMessage();
  78.             }  
  79.         }
  80.         public function insert($table, $data) {
  81.             try {
  82.                 $field = $value = '';
  83.                 foreach($data as $f=>$v) {
  84.                     $field .= ','.str_replace(':','',$v);
  85.                     $value .= ','.$v;
  86.                 }
  87.                 $field = substr($field,1);
  88.                 $value = substr($value,1);
  89.                 $query = 'INSERT INTO '.$table.' ('.$field.') VALUES ('.$value.')';
  90.                 $st = $this->con->prepare($query);
  91.                 $st->execute($data);
  92.                 $this->success = true;
  93.             }
  94.             catch (PDOException $e) {
  95.                 $this->success = false;
  96.                 $this->message = $e->getMessage();
  97.             }
  98.         }
  99.  
  100.         public function update($table, $data, $where, $condition='AND') {
  101.             try {
  102.                 $field = $value = '';
  103.                 foreach($data as $f=>$v) {
  104.                     $field .= ','.str_replace(':','',$f).'='.$f;
  105.                 }
  106.                 foreach($where as $f=>$v) {
  107.                     $value .= ' && '.str_replace(':','',$f).'='.$f;
  108.                 }
  109.                 $field = substr($field,1);
  110.                 $value = substr($value,4);
  111.                 $value = str_replace('&&',$condition,$value);
  112.                 $query = 'UPDATE '.$table.' SET '.$field.' WHERE '.$value;
  113.                 $st = $this->con->prepare($query);
  114.                 $st->execute(array_merge($data,$where));
  115.                 $this->success = true;
  116.             }
  117.             catch (PDOException $e) {
  118.                 $this->success = false;
  119.                 $this->message = $e->getMessage();
  120.             }
  121.         }
  122.  
  123.         public function delete($table, $where, $condition='AND') {
  124.             try {
  125.                 $value = '';
  126.                 foreach($where as $f=>$v) {
  127.                     $value .= ' && '.str_replace(':','',$f).'='.$f;
  128.                 }
  129.                 $value = substr($value,4);
  130.                 $value = str_replace('&&',$condition,$value);
  131.                 $query = 'DELETE FROM '.$table.' WHERE '.$value;
  132.                 $st = $this->con->prepare($query);
  133.                 $st->execute($where);
  134.                 $this->success = true;
  135.             }
  136.             catch (PDOException $e) {
  137.                 $this->success = false;
  138.                 $this->message = $e->getMessage();
  139.             }
  140.         }
  141.  
  142.         public function select($table, $data='*', $where=null, $group=null, $order=null, $limit=null, $condition='AND') {
  143.             try {
  144.                 $output = array();
  145.                 $value = '';
  146.                 $query = 'SELECT '.$data.' FROM '.$table;
  147.                 if($where!=null) {
  148.                     foreach($where as $f=>$v) {
  149.                         $value .= ' && '.str_replace(':','',$f).'='.$f;
  150.                     }
  151.                     $value = ' WHERE '.substr($value,4);
  152.                     $value = str_replace('&&',$condition,$value);
  153.                     $query .= $value;
  154.                 }
  155.                 ($group!=null) ? $query .= ' GROUP BY '.$group :null;
  156.                 ($order!=null) ? $query .= ' ORDER BY '.$order :null;
  157.                 ($limit!=null) ? $query .= ' LIMIT '.$limit :null;
  158.                 $st = $this->con->prepare($query);
  159.                 $st->execute($where);
  160.                 $this->success = true;
  161.                 while($result = $st->fetch()) {
  162.                     $output[] = $result;
  163.                 }
  164.                 $output = str_replace("'","&#39;",$output);
  165.                 return $output;
  166.             }
  167.             catch (PDOException $e) {
  168.                 $this->success = false;
  169.                 $this->message = $e->getMessage();
  170.             }
  171.         }
  172.  
  173.         public function selectAssoc($table, $data='*', $where=null, $group=null, $order=null, $limit=null, $condition='AND') {
  174.             try {
  175.                 $output = array();
  176.                 $value = '';
  177.                 $query = 'SELECT '.$data.' FROM '.$table;
  178.                 if($where!=null) {
  179.                     foreach($where as $f=>$v) {
  180.                         $value .= ' && '.str_replace(':','',$f).'='.$f;
  181.                     }
  182.                     $value = ' WHERE '.substr($value,4);
  183.                     $value = str_replace('&&',$condition,$value);
  184.                     $query .= $value;
  185.                 }
  186.                 ($group!=null) ? $query .= ' GROUP BY '.$group :null;
  187.                 ($order!=null) ? $query .= ' ORDER BY '.$order :null;
  188.                 ($limit!=null) ? $query .= ' LIMIT '.$limit :null;
  189.                 $st = $this->con->prepare($query);
  190.                 $st->execute($where);
  191.                 $this->success = true;
  192.                 while($result = $st->fetch(PDO::FETCH_ASSOC)) {
  193.                     $output[] = $result;
  194.                 }
  195.                 $output = str_replace("'","&#39;",$output);
  196.                 return $output;
  197.             }
  198.             catch (PDOException $e) {
  199.                 $this->success = false;
  200.                 $this->message = $e->getMessage();
  201.             }
  202.         }
  203.  
  204.         public function fetch($table, $data='*', $where=null, $group=null, $order=null, $limit=null, $condition='AND') {
  205.             try {
  206.                 $value = '';
  207.                 $query = 'SELECT '.$data.' FROM '.$table;
  208.                 if($where!=null) {
  209.                     foreach($where as $f=>$v) {
  210.                         $value .= ' && '.str_replace(':','',$f).'='.$f;
  211.                     }
  212.                     $value = ' WHERE '.substr($value,4);
  213.                     $value = str_replace('&&',$condition,$value);
  214.                     $query .= $value;
  215.                 }
  216.                 ($group!=null) ? $query .= ' GROUP BY '.$group :null;
  217.                 ($order!=null) ? $query .= ' ORDER BY '.$order :null;
  218.                 ($limit!=null) ? $query .= ' LIMIT '.$limit :null;
  219.                 $st = $this->con->prepare($query);
  220.                 $st->execute($where);
  221.                 $output = $st->fetch();
  222.                 $this->success = true;
  223.                 if($output=='') {
  224.                     $output = array();
  225.                     $field = explode(',',$data);
  226.                     $i = 0;
  227.                     foreach($field as $fil) {
  228.                         $output = array_merge($output,array($i=>''));
  229.                         $output = array_merge($output,array($fil=>''));
  230.                         $i++;
  231.                     }
  232.                 } else {
  233.                     $output = str_replace("'","&#39;",$output);
  234.                 }
  235.                 return $output;
  236.             }
  237.             catch (PDOException $e) {
  238.                 $this->success = false;
  239.                 $this->message = $e->getMessage();
  240.             }
  241.         }
  242.  
  243.         public function fetchAssoc($table, $data='*', $where=null, $group=null, $order=null, $limit=null, $condition='AND') {
  244.             try {
  245.                 $value = '';
  246.                 $query = 'SELECT '.$data.' FROM '.$table;
  247.                 if($where!=null) {
  248.                     foreach($where as $f=>$v) {
  249.                         $value .= ' && '.str_replace(':','',$f).'='.$f;
  250.                     }
  251.                     $value = ' WHERE '.substr($value,4);
  252.                     $value = str_replace('&&',$condition,$value);
  253.                     $query .= $value;
  254.                 }
  255.                 ($group!=null) ? $query .= ' GROUP BY '.$group :null;
  256.                 ($order!=null) ? $query .= ' ORDER BY '.$order :null;
  257.                 ($limit!=null) ? $query .= ' LIMIT '.$limit :null;
  258.                 $st = $this->con->prepare($query);
  259.                 $st->execute($where);
  260.                 $output = $st->fetch(PDO::FETCH_ASSOC);
  261.                 $this->success = true;
  262.                 if($output=='') {
  263.                     $output = array();
  264.                     $field = explode(',',$data);
  265.                     $i = 0;
  266.                     foreach($field as $fil) {
  267.                         $output = array_merge($output,array($i=>''));
  268.                         $output = array_merge($output,array($fil=>''));
  269.                         $i++;
  270.                     }
  271.                 } else {
  272.                     $output = str_replace("'","&#39;",$output);
  273.                 }
  274.                 return $output;
  275.             }
  276.             catch (PDOException $e) {
  277.                 $this->success = false;
  278.                 $this->message = $e->getMessage();
  279.             }
  280.         }
  281.  
  282.         public function fieldcount($table, $data='*', $where=null, $group=null, $order=null, $limit=null, $condition='AND') {
  283.             try {
  284.                 $value = '';
  285.                 $query = 'SELECT COUNT('.$data.') FROM '.$table;
  286.                 if($where!=null) {
  287.                     foreach($where as $f=>$v) {
  288.                         $value .= ' && '.str_replace(':','',$f).'='.$f;
  289.                     }
  290.                     $value = ' WHERE '.substr($value,4);
  291.                     $value = str_replace('&&',$condition,$value);
  292.                     $query .= $value;
  293.                 }
  294.                 ($group!=null) ? $query .= ' GROUP BY '.$group :null;
  295.                 ($order!=null) ? $query .= ' ORDER BY '.$order :null;
  296.                 ($limit!=null) ? $query .= ' LIMIT '.$limit :null;
  297.                 $st = $this->con->prepare($query);
  298.                 $st->execute($where);
  299.                 $this->success = true;
  300.                 $output = $st->columnCount();
  301.                 return $output;
  302.             }
  303.             catch (PDOException $e) {
  304.                 $this->success = false;
  305.                 $this->message = $e->getMessage();
  306.             }
  307.         }
  308.  
  309.         public function rowcount($table, $data='*', $where=null, $group=null, $order=null, $limit=null, $condition='AND') {
  310.             try {
  311.                 $value = '';
  312.                 $query = 'SELECT COUNT('.$data.') FROM '.$table;
  313.                 if($where!=null) {
  314.                     foreach($where as $f=>$v) {
  315.                         $value .= ' && '.str_replace(':','',$f).'='.$f;
  316.                     }
  317.                     $value = ' WHERE '.substr($value,4);
  318.                     $value = str_replace('&&',$condition,$value);
  319.                     $query .= $value;
  320.                 }
  321.                 ($group!=null) ? $query .= ' GROUP BY '.$group :null;
  322.                 ($order!=null) ? $query .= ' ORDER BY '.$order :null;
  323.                 ($limit!=null) ? $query .= ' LIMIT '.$limit :null;
  324.                 $st = $this->con->prepare($query);
  325.                 $st->execute($where);
  326.                 $this->success = true;
  327.                 $output = $st->fetchColumn(0);
  328.                 return $output;
  329.             }
  330.             catch (PDOException $e) {
  331.                 $this->success = false;
  332.                 $this->message = $e->getMessage();
  333.             }
  334.         }
  335.  
  336.         public function rowsum($table, $data='*', $where=null, $group=null, $order=null, $limit=null, $condition='AND') {
  337.             try {
  338.                 $value = '';
  339.                 $query = 'SELECT SUM('.$data.') FROM '.$table;
  340.                 if($where!=null) {
  341.                     foreach($where as $f=>$v) {
  342.                         $value .= ' && '.str_replace(':','',$f).'='.$f;
  343.                     }
  344.                     $value = ' WHERE '.substr($value,4);
  345.                     $value = str_replace('&&',$condition,$value);
  346.                     $value = str_replace("!=","<>",$value);
  347.                     $query .= $value;
  348.                 }
  349.                 ($group!=null) ? $query .= ' GROUP BY '.$group :null;
  350.                 ($order!=null) ? $query .= ' ORDER BY '.$order :null;
  351.                 ($limit!=null) ? $query .= ' LIMIT '.$limit :null;
  352.                 $st = $this->con->prepare($query);
  353.                 $st->execute($where);
  354.                 $this->success = true;
  355.                 $output = $st->fetchColumn(0);
  356.                 return $output;
  357.             }
  358.             catch (PDOException $e) {
  359.                 $this->success = false;
  360.                 $this->message = $e->getMessage();
  361.             }
  362.         }
  363.  
  364.         public function truncate($table) {
  365.             try {
  366.                 $query = 'TRUNCATE '.$table;
  367.                 $st = $this->con->prepare($query);
  368.                 $st->execute();
  369.                 $this->success = true;
  370.             }
  371.             catch (PDOException $e) {
  372.                 $this->success = false;
  373.                 $this->message = $e->getMessage();
  374.             }
  375.         }
  376.  
  377.         public function query($query) {
  378.             try {
  379.                 $st = $this->con->prepare($query);
  380.                 $st->execute();
  381.                 return $st->fetchAll();
  382.                 $this->success = true;
  383.             }
  384.             catch (PDOException $e) {
  385.                 $this->success = false;
  386.                 $this->message = $e->getMessage();
  387.             }
  388.         }
  389.  
  390.         public function dbsize() {
  391.             $output = $this->query("SELECT sum( data_length + index_length ) AS dbsize FROM information_schema.TABLES WHERE table_schema='".$this->debe."'");
  392.             return $output[0][0];
  393.         }
  394.     }
  395. ?>
  396.  
Add Comment
Please, Sign In to add comment