Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. <?php
  2. class Database{
  3.  
  4. //variable untuk property
  5. private static $INSTANCE;
  6. private $mysqli,
  7. $hostname = "localhost",
  8. $username = "root",
  9. $password = "gunawan02",
  10. $dbname = "loginOOP";
  11.  
  12. ///fungsi yang dijalankan di awal pada saat object dibuat
  13. public function __construct(){
  14. $this->mysqli = new mysqli($this->hostname , $this->username , $this->password , $this->dbname);
  15. if (mysqli_connect_error()) {
  16. die("cant connect database");
  17. }
  18. }
  19.  
  20.  
  21. //singleton pattern , membuat koneksi db tidak berulang" dipanggil
  22. public static function getInstance(){
  23. if (!isset ( self::$INSTANCE )) {
  24. self::$INSTANCE = new Database();
  25. }
  26. return self::$INSTANCE;
  27. }
  28.  
  29. //function insert untuk register
  30. public function insert_table($table , $arr= array()){
  31. $kolom = implode(",",array_keys($arr));
  32. $arrayvalue = [];
  33. $i = 0;
  34. foreach ($arr as $key => $value) {
  35. if (is_int($value)) {
  36. $arrayvalue[$i] = $this->escape($value);
  37. }else {
  38. $arrayvalue[$i] = "'".$this->escape($value)."'";
  39. }
  40. $i++;
  41. }
  42. $value = implode(",",$arrayvalue);
  43. $query = "INSERT INTO $table($kolom) VALUES ($value)";
  44. return $this->run_query($query , "error cant connect query");
  45. }
  46.  
  47. //function untuk cek login , atau proses pengecekan login
  48. public function get_info($table,$kolom,$value){
  49. if (!is_int($value)) {
  50. $value = "'".$value."'";
  51. }
  52. $query = "SELECT * FROM $table WHERE $kolom = $value";
  53. $result = $this->mysqli->query($query);
  54.  
  55. while ($row = $result->fetch_assoc()) {
  56. return $row;
  57. }
  58. }
  59.  
  60.  
  61. /////////////////////////////////////////////////////////////////bagian error//////////////////////////////////////////////
  62. public function update($table, $fields, $id)
  63. {
  64. //mengambil nilai
  65. $valueArrays = array();
  66. $i = 0;
  67.  
  68. foreach($fields as $key=>$values){
  69. if( is_int($values) ){
  70. $valueArrays[$i] = $key . "=" . $this->escape($values);
  71. }else{
  72. $valueArrays[$i] = $key . "='" . $this->escape($values) . "'";
  73. }
  74.  
  75. $i++;
  76. }
  77. $values = implode(",", $valueArrays);
  78.  
  79. $query = "UPDATE $table SET $values WHERE id = $id";
  80. die($query);
  81. return $this->run_query($query, 'masalah saat mengupdate data');
  82. }
  83.  
  84.  
  85. //untuk escape string
  86. public function escape($string){
  87. return $this->mysqli->real_escape_string(htmlentities($string));
  88. }
  89.  
  90. //membuat error dan run query
  91. public function run_query($query,$catch){
  92. if ($this->mysqli->query($query)) return true;
  93. else die($catch);
  94. }
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108. }
  109. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement