Advertisement
Guest User

Untitled

a guest
Jun 25th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.15 KB | None | 0 0
  1. <?php
  2.  
  3. // Database Access Object
  4. class dao {
  5. private $db = NULL;
  6. private $connection_string = NULL;
  7. private $db_type = DB_TYPE;
  8. private $db_path = DB_PATH;
  9. private $db_host = DB_HOST;
  10. private $db_user = DB_USER;
  11. private $db_pass = DB_PASS;
  12. private $db_name = DB_NAME;
  13. private $con = false;
  14.  
  15. public function __construct($db_user = DB_USER, $db_pass = DB_PASS, $db_name = DB_NAME, $db_type = 'mysql', $db_path = DB_PATH, $db_host = 'localhost')
  16. {
  17. $this->db_host = $db_host;
  18. $this->db_user = $db_user;
  19. $this->db_pass = $db_pass;
  20. $this->db_name = $db_name;
  21. $this->db_path = $db_path;
  22. $this->db_type = $db_type;
  23.  
  24. switch($this->db_type){
  25. case "mysql":
  26. $this->connection_string = "mysql:host=".$db_host.";dbname=".$db_name;
  27. break;
  28.  
  29. case "sqlite":
  30. $this->connection_string = "sqlite:".$db_path;
  31. break;
  32.  
  33. case "oracle":
  34. $this->connection_string = "OCI:dbname=".$db_name.";charset=UTF-8";
  35. break;
  36.  
  37. case "dblib":
  38. $this->connection_string = "dblib:host=".$db_host.";dbname=".$db_name;
  39. break;
  40.  
  41. case "postgresql":
  42. $this->connection_string = "pgsql:host=".$db_host." dbname=".$db_name;
  43. break;
  44. }
  45. return $this;
  46. }
  47.  
  48. public function connect() {
  49. if(!$this->con) {
  50. try {
  51. $this->db = new PDO($this->connection_string,$this->db_user, $this->db_pass);
  52. $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  53. $this->con = true;
  54. return $this->con;
  55. } catch (PDOException $e) {
  56. return $e->getMessage();
  57. }
  58. } else {
  59. return true;
  60. }
  61. }
  62.  
  63. public function disconnect() {
  64. if($this->con) {
  65. unset($this->db);$this->con = false;
  66. return true;
  67. }
  68. }
  69.  
  70. /**
  71. $db = new dao('username','password','database');
  72. $db->connect();
  73. if ($db->connect()) {
  74. echo "tersambung dengan database";
  75. } else {
  76. echo "gagal tersambung"
  77. $db->disconnect(); // kalau mau disconnect
  78. **/
  79.  
  80. // SELECT
  81. public function select($table, $rows = '*', $where = null, $order = null) {
  82. if($this->tableExists($table)) {
  83. $q = 'SELECT '.$rows.' FROM '.$table;
  84. if($where != null)
  85. $q .= ' WHERE '.$where;
  86. if($order != null)
  87. $q .= ' ORDER BY '.$order;
  88. $this->numResults = null;
  89. try {
  90. $sql = $this->db->prepare($q);
  91. $sql->execute();
  92. $this->result = $sql->fetchAll(PDO::FETCH_ASSOC);
  93. $this->numResults = count($this->result);
  94. $this->numResults === 0 ? $this->result = null : true ;
  95. return true;
  96. } catch (PDOException $e) {
  97. return $e->getMessage().''.$e->getTraceAsString().'';
  98. }
  99. }
  100. }
  101.  
  102. public function getResult(){
  103. return $this->result;
  104. }
  105.  
  106. /**
  107. $result = $db->select('users');
  108. if ($result === true){
  109. echo "result select ";
  110. print_r($db->getResult());
  111. } else {
  112. var_dump($result);
  113. }
  114. **/
  115.  
  116. // INSERT
  117. public function insert ($table,$values,$rows = null) {
  118. $insert = 'INSERT INTO '.$table;
  119. if($rows != null) {
  120. $insert .= ' ('.$rows.')';
  121. }
  122.  
  123. for($i = 0; $i < count($values); $i++) {
  124. if(is_string($values[$i]))
  125. $values[$i] = '"'.$values[$i].'"';
  126. }
  127.  
  128. $values = implode(',',$values);
  129. $insert .= ' VALUES ('.$values.')';
  130.  
  131. try {
  132. $ins = $this->db->prepare($insert);
  133. $ins->execute();
  134. return true;
  135. } catch (PDOException $e) {
  136. return $e->getMessage();
  137. }
  138. }
  139.  
  140. /**
  141. $result = $db->insert("user", array("","test123","test","0"), "id,username,password,active");
  142. **/
  143.  
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement