Advertisement
kura2yamato

perbaikan sql lib 112

Sep 23rd, 2020 (edited)
1,313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. class SQL
  3. {
  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.     {
  19.         if ($h == null)
  20.         {
  21.             $this->server = 'mysql:host=' . $this->host . ';dbname=' . $this->debe;
  22.         }
  23.         else
  24.         {
  25.             $this->server = 'mysql:host=' . $h . ';dbname=' . $d;
  26.             $this->user = $u;
  27.             $this->pass = $p;
  28.         }
  29.  
  30.         try
  31.         {
  32.             $this->con = new PDO($this->server, $this->user, $this->pass, $this->attribute);
  33.             $this->success = true;
  34.         }
  35.         catch(PDOException $e)
  36.         {
  37.             $this->success = false;
  38.             $this->message = $e->getMessage();
  39.         }
  40.     }
  41.  
  42.     public function dbstatus()
  43.     {
  44.         if ($this->con == null)
  45.         {
  46.             return $this->message;
  47.         }
  48.     }
  49.  
  50.     public function response()
  51.     {
  52.         if ($this->success == false)
  53.         {
  54.             return $this->message;
  55.         }
  56.         else
  57.         {
  58.             return 1;
  59.         }
  60.     }
  61.  
  62.     public function close()
  63.     {
  64.         $this->con = null;
  65.     }
  66.  
  67.     public function insertid()
  68.     {
  69.         return $this
  70.             ->con
  71.             ->lastInsertId();
  72.     }
  73.     /*
  74.     if any string with `field` will be replace with ``
  75.     */
  76.     public function insertUPLOAD($table, $data)
  77.     {
  78.         try
  79.         {
  80.             $fields = $values = array();
  81.             foreach ($data as $f => $v)
  82.             {
  83.                 $fields[]= '`' . str_replace(':', '', str_replace("field", "", $f)) . '`';
  84.                 $values[]= "'" . addslashes($v)."'";
  85.             }
  86.            
  87.             $field = implode(",", $fields );
  88.             $value = implode(",", $values );
  89.             $query = 'INSERT INTO ' . $table . ' (' . $field . ') VALUES (' . $value . ')';
  90.             $st = $this
  91.                 ->con
  92.                 ->prepare($query);
  93.             $st->execute($data);
  94.             $this->success = true;
  95.         }
  96.         catch(PDOException $e)
  97.         {
  98.             $this->success = false;
  99.             $this->message = "error:".$e->getMessage()."\nSql:".$query;
  100.         }
  101.     }
  102.     public function insert($table, $data)
  103.     {
  104.         try
  105.         {
  106.             $field = $value = '';
  107.             foreach ($data as $f => $v)
  108.             {
  109.                 $field .= ',' . str_replace(':', '', $v);
  110.                 $value .= ',' . $v;
  111.             }
  112.             $field = substr($field, 1);
  113.             $value = substr($value, 1);
  114.             $query = 'INSERT INTO ' . $table . ' (' . $field . ') VALUES (' . $value . ')';
  115.             $st = $this
  116.                 ->con
  117.                 ->prepare($query);
  118.             $st->execute($data);
  119.             $this->success = true;
  120.         }
  121.         catch(PDOException $e)
  122.         {
  123.             $this->success = false;
  124.             $this->message = $e->getMessage();
  125.         }
  126.     }
  127.  
  128.     public function update($table, $data, $where, $condition = 'AND')
  129.     {
  130.         try
  131.         {
  132.             $field = $value = '';
  133.             foreach ($data as $f => $v)
  134.             {
  135.                 $field .= ',' . str_replace(':', '', $f) . '=' . $f;
  136.             }
  137.             foreach ($where as $f => $v)
  138.             {
  139.                 $value .= ' && ' . str_replace(':', '', $f) . '=' . $f;
  140.             }
  141.             $field = substr($field, 1);
  142.             $value = substr($value, 4);
  143.             $value = str_replace('&&', $condition, $value);
  144.             $query = 'UPDATE ' . $table . ' SET ' . $field . ' WHERE ' . $value;
  145.             $st = $this
  146.                 ->con
  147.                 ->prepare($query);
  148.             $st->execute(array_merge($data, $where));
  149.             $this->success = true;
  150.         }
  151.         catch(PDOException $e)
  152.         {
  153.             $this->success = false;
  154.             $this->message = $e->getMessage();
  155.         }
  156.     }
  157.  
  158.     public function delete($table, $where, $condition = 'AND')
  159.     {
  160.         try
  161.         {
  162.             $value = '';
  163.             foreach ($where as $f => $v)
  164.             {
  165.                 $value .= ' && ' . str_replace(':', '', $f) . '=' . $f;
  166.             }
  167.             $value = substr($value, 4);
  168.             $value = str_replace('&&', $condition, $value);
  169.             $query = 'DELETE FROM ' . $table . ' WHERE ' . $value;
  170.             $st = $this
  171.                 ->con
  172.                 ->prepare($query);
  173.             $st->execute($where);
  174.             $this->success = true;
  175.         }
  176.         catch(PDOException $e)
  177.         {
  178.             $this->success = false;
  179.             $this->message = $e->getMessage();
  180.         }
  181.     }
  182.  
  183.     public function select($table, $data = '*', $where = null, $group = null, $order = null, $limit = null, $condition = 'AND')
  184.     {
  185.         try
  186.         {
  187.             $output = array();
  188.             $value = '';
  189.             $query = 'SELECT ' . $data . ' FROM ' . $table;
  190.             if ($where != null)
  191.             {
  192.                 foreach ($where as $f => $v)
  193.                 {
  194.                     $value .= ' && ' . str_replace(':', '', $f) . '=' . $f;
  195.                 }
  196.                 $value = ' WHERE ' . substr($value, 4);
  197.                 $value = str_replace('&&', $condition, $value);
  198.                 $query .= $value;
  199.             }
  200.             ($group != null) ? $query .= ' GROUP BY ' . $group : null;
  201.             ($order != null) ? $query .= ' ORDER BY ' . $order : null;
  202.             ($limit != null) ? $query .= ' LIMIT ' . $limit : null;
  203.             $st = $this
  204.                 ->con
  205.                 ->prepare($query);
  206.             $st->execute($where);
  207.             $this->success = true;
  208.             while ($result = $st->fetch())
  209.             {
  210.                 $output[] = $result;
  211.             }
  212.             $output = str_replace("'", "&#39;", $output);
  213.             return $output;
  214.         }
  215.         catch(PDOException $e)
  216.         {
  217.             $this->success = false;
  218.             $this->message = $e->getMessage();
  219.         }
  220.     }
  221.  
  222.     public function selectAssoc($table, $data = '*', $where = null, $group = null, $order = null, $limit = null, $condition = 'AND')
  223.     {
  224.         try
  225.         {
  226.             $output = array();
  227.             $value = '';
  228.             $query = 'SELECT ' . $data . ' FROM ' . $table;
  229.             if ($where != null)
  230.             {
  231.                 foreach ($where as $f => $v)
  232.                 {
  233.                     $value .= ' && ' . str_replace(':', '', $f) . '=' . $f;
  234.                 }
  235.                 $value = ' WHERE ' . substr($value, 4);
  236.                 $value = str_replace('&&', $condition, $value);
  237.                 $query .= $value;
  238.             }
  239.             ($group != null) ? $query .= ' GROUP BY ' . $group : null;
  240.             ($order != null) ? $query .= ' ORDER BY ' . $order : null;
  241.             ($limit != null) ? $query .= ' LIMIT ' . $limit : null;
  242.             $st = $this
  243.                 ->con
  244.                 ->prepare($query);
  245.             $st->execute($where);
  246.             $this->success = true;
  247.             while ($result = $st->fetch(PDO::FETCH_ASSOC))
  248.             {
  249.                 $output[] = $result;
  250.             }
  251.             $output = str_replace("'", "&#39;", $output);
  252.             return $output;
  253.         }
  254.         catch(PDOException $e)
  255.         {
  256.             $this->success = false;
  257.             $this->message = $e->getMessage();
  258.         }
  259.     }
  260.  
  261.     public function fetch($table, $data = '*', $where = null, $group = null, $order = null, $limit = null, $condition = 'AND')
  262.     {
  263.         try
  264.         {
  265.             $value = '';
  266.             $query = 'SELECT ' . $data . ' FROM ' . $table;
  267.             if ($where != null)
  268.             {
  269.                 foreach ($where as $f => $v)
  270.                 {
  271.                     $value .= ' && ' . str_replace(':', '', $f) . '=' . $f;
  272.                 }
  273.                 $value = ' WHERE ' . substr($value, 4);
  274.                 $value = str_replace('&&', $condition, $value);
  275.                 $query .= $value;
  276.             }
  277.             ($group != null) ? $query .= ' GROUP BY ' . $group : null;
  278.             ($order != null) ? $query .= ' ORDER BY ' . $order : null;
  279.             ($limit != null) ? $query .= ' LIMIT ' . $limit : null;
  280.             $st = $this
  281.                 ->con
  282.                 ->prepare($query);
  283.             $st->execute($where);
  284.             $output = $st->fetch();
  285.             $this->success = true;
  286.             if ($output == '')
  287.             {
  288.                 $output = array();
  289.                 $field = explode(',', $data);
  290.                 $i = 0;
  291.                 foreach ($field as $fil)
  292.                 {
  293.                     $output = array_merge($output, array(
  294.                         $i => ''
  295.                     ));
  296.                     $output = array_merge($output, array(
  297.                         $fil => ''
  298.                     ));
  299.                     $i++;
  300.                 }
  301.             }
  302.             else
  303.             {
  304.                 $output = str_replace("'", "&#39;", $output);
  305.             }
  306.             return $output;
  307.         }
  308.         catch(PDOException $e)
  309.         {
  310.             $this->success = false;
  311.             $this->message = $e->getMessage();
  312.         }
  313.     }
  314.  
  315.     public function fetchAssoc($table, $data = '*', $where = null, $group = null, $order = null, $limit = null, $condition = 'AND')
  316.     {
  317.         try
  318.         {
  319.             $value = '';
  320.             $query = 'SELECT ' . $data . ' FROM ' . $table;
  321.             if ($where != null)
  322.             {
  323.                 foreach ($where as $f => $v)
  324.                 {
  325.                     $value .= ' && ' . str_replace(':', '', $f) . '=' . $f;
  326.                 }
  327.                 $value = ' WHERE ' . substr($value, 4);
  328.                 $value = str_replace('&&', $condition, $value);
  329.                 $query .= $value;
  330.             }
  331.             ($group != null) ? $query .= ' GROUP BY ' . $group : null;
  332.             ($order != null) ? $query .= ' ORDER BY ' . $order : null;
  333.             ($limit != null) ? $query .= ' LIMIT ' . $limit : null;
  334.             $st = $this
  335.                 ->con
  336.                 ->prepare($query);
  337.             $st->execute($where);
  338.             $output = $st->fetch(PDO::FETCH_ASSOC);
  339.             $this->success = true;
  340.             if ($output == '')
  341.             {
  342.                 $output = array();
  343.                 $field = explode(',', $data);
  344.                 $i = 0;
  345.                 foreach ($field as $fil)
  346.                 {
  347.                     $output = array_merge($output, array(
  348.                         $i => ''
  349.                     ));
  350.                     $output = array_merge($output, array(
  351.                         $fil => ''
  352.                     ));
  353.                     $i++;
  354.                 }
  355.             }
  356.             else
  357.             {
  358.                 $output = str_replace("'", "&#39;", $output);
  359.             }
  360.             return $output;
  361.         }
  362.         catch(PDOException $e)
  363.         {
  364.             $this->success = false;
  365.             $this->message = $e->getMessage();
  366.         }
  367.     }
  368.  
  369.     public function fieldcount($table, $data = '*', $where = null, $group = null, $order = null, $limit = null, $condition = 'AND')
  370.     {
  371.         try
  372.         {
  373.             $value = '';
  374.             $query = 'SELECT COUNT(' . $data . ') FROM ' . $table;
  375.             if ($where != null)
  376.             {
  377.                 foreach ($where as $f => $v)
  378.                 {
  379.                     $value .= ' && ' . str_replace(':', '', $f) . '=' . $f;
  380.                 }
  381.                 $value = ' WHERE ' . substr($value, 4);
  382.                 $value = str_replace('&&', $condition, $value);
  383.                 $query .= $value;
  384.             }
  385.             ($group != null) ? $query .= ' GROUP BY ' . $group : null;
  386.             ($order != null) ? $query .= ' ORDER BY ' . $order : null;
  387.             ($limit != null) ? $query .= ' LIMIT ' . $limit : null;
  388.             $st = $this
  389.                 ->con
  390.                 ->prepare($query);
  391.             $st->execute($where);
  392.             $this->success = true;
  393.             $output = $st->columnCount();
  394.             return $output;
  395.         }
  396.         catch(PDOException $e)
  397.         {
  398.             $this->success = false;
  399.             $this->message = $e->getMessage();
  400.         }
  401.     }
  402.  
  403.     public function rowcount($table, $data = '*', $where = null, $group = null, $order = null, $limit = null, $condition = 'AND')
  404.     {
  405.         try
  406.         {
  407.             $value = '';
  408.             $query = 'SELECT COUNT(' . $data . ') FROM ' . $table;
  409.             if ($where != null)
  410.             {
  411.                 foreach ($where as $f => $v)
  412.                 {
  413.                     $value .= ' && ' . str_replace(':', '', $f) . '=' . $f;
  414.                 }
  415.                 $value = ' WHERE ' . substr($value, 4);
  416.                 $value = str_replace('&&', $condition, $value);
  417.                 $query .= $value;
  418.             }
  419.             ($group != null) ? $query .= ' GROUP BY ' . $group : null;
  420.             ($order != null) ? $query .= ' ORDER BY ' . $order : null;
  421.             ($limit != null) ? $query .= ' LIMIT ' . $limit : null;
  422.             $st = $this
  423.                 ->con
  424.                 ->prepare($query);
  425.             $st->execute($where);
  426.             $this->success = true;
  427.             $output = $st->fetchColumn(0);
  428.             return $output;
  429.         }
  430.         catch(PDOException $e)
  431.         {
  432.             $this->success = false;
  433.             $this->message = $e->getMessage();
  434.         }
  435.     }
  436.  
  437.     public function rowsum($table, $data = '*', $where = null, $group = null, $order = null, $limit = null, $condition = 'AND')
  438.     {
  439.         try
  440.         {
  441.             $value = '';
  442.             $query = 'SELECT SUM(' . $data . ') FROM ' . $table;
  443.             if ($where != null)
  444.             {
  445.                 foreach ($where as $f => $v)
  446.                 {
  447.                     $value .= ' && ' . str_replace(':', '', $f) . '=' . $f;
  448.                 }
  449.                 $value = ' WHERE ' . substr($value, 4);
  450.                 $value = str_replace('&&', $condition, $value);
  451.                 $value = str_replace("!=", "<>", $value);
  452.                 $query .= $value;
  453.             }
  454.             ($group != null) ? $query .= ' GROUP BY ' . $group : null;
  455.             ($order != null) ? $query .= ' ORDER BY ' . $order : null;
  456.             ($limit != null) ? $query .= ' LIMIT ' . $limit : null;
  457.             $st = $this
  458.                 ->con
  459.                 ->prepare($query);
  460.             $st->execute($where);
  461.             $this->success = true;
  462.             $output = $st->fetchColumn(0);
  463.             return $output;
  464.         }
  465.         catch(PDOException $e)
  466.         {
  467.             $this->success = false;
  468.             $this->message = $e->getMessage();
  469.         }
  470.     }
  471.  
  472.     public function truncate($table)
  473.     {
  474.         try
  475.         {
  476.             $query = 'TRUNCATE ' . $table;
  477.             $st = $this
  478.                 ->con
  479.                 ->prepare($query);
  480.             $st->execute();
  481.             $this->success = true;
  482.         }
  483.         catch(PDOException $e)
  484.         {
  485.             $this->success = false;
  486.             $this->message = $e->getMessage();
  487.         }
  488.     }
  489.  
  490.     public function query($query)
  491.     {
  492.         try
  493.         {
  494.             $st = $this
  495.                 ->con
  496.                 ->prepare($query);
  497.             $st->execute();
  498.             return $st->fetchAll();
  499.             $this->success = true;
  500.         }
  501.         catch(PDOException $e)
  502.         {
  503.             $this->success = false;
  504.             $this->message = $e->getMessage();
  505.         }
  506.     }
  507.  
  508.     public function dbsize()
  509.     {
  510.         $output = $this->query("SELECT sum( data_length + index_length ) AS dbsize FROM information_schema.TABLES WHERE table_schema='" . $this->debe . "'");
  511.         return $output[0][0];
  512.     }
  513. }
  514. ?>
  515.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement