Guest User

Untitled

a guest
Jul 16th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1. <?php
  2. # PDO Wrapper, supporting MySQL and Sqlite
  3. # Usage:
  4. # $db = new db();
  5. #
  6. # // table, data
  7. # $db->create('users', array(
  8. # 'fname' => 'john',
  9. # 'lname' => 'doe'
  10. # ));
  11. #
  12. # // table, where, where-bind
  13. # $db->read('users', "fname LIKE :search", array(
  14. # ':search' => 'j%'
  15. # ));
  16. #
  17. # // table, data, where, where-bind
  18. # $db->update('users', array(
  19. # 'fname' => 'jame'
  20. # ), 'gender = :gender', array(
  21. # ':gender' => 'female'
  22. # ));
  23. #
  24. # // table, where, where-bind
  25. # $db->delete('users', 'lname = :lname', array(
  26. # ':lname' => 'doe'
  27. # ));
  28.  
  29. class db
  30. {
  31. private $config = array(
  32. # "dbdriver" => "sqlite",
  33. # "sqlitedb" => "path/to/db.sqlite"
  34.  
  35. "dbdriver" => "mysql",
  36. "dbuser" => "root",
  37. "dbpass" => "",
  38. "dbname" => "test"
  39. );
  40.  
  41. function db() {
  42. $dbhost = $this->config['dbhost'];
  43. $dbuser = $this->config['dbuser'];
  44. $dbpass = $this->config['dbpass'];
  45. $dbname = $this->config['dbname'];
  46.  
  47. # $sqlitedb = $this->config['sqlitedb'];
  48.  
  49. $options = array(
  50. PDO::ATTR_PERSISTENT => true,
  51. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
  52. );
  53.  
  54. try {
  55. switch($this->config["dbdriver"]) {
  56. case "sqlite":
  57. $conn = "sqlite:{$sqlitedb}";
  58. break;
  59. case "mysql":
  60. $conn = "mysql:host={$dbhost};dbname={$dbname}";
  61. break;
  62. default:
  63. echo "Unsuportted DB Driver! Check the configuration.";
  64. exit(1);
  65. }
  66.  
  67. $this->db = new PDO($conn, $dbuser, $dbpass, $options);
  68.  
  69. } catch(PDOException $e) {
  70. echo $e->getMessage(); exit(1);
  71. }
  72. }
  73.  
  74. function run($sql, $bind=array()) {
  75. $sql = trim($sql);
  76.  
  77. try {
  78.  
  79. $result = $this->db->prepare($sql);
  80. $result->execute($bind);
  81. return $result;
  82.  
  83. } catch (PDOException $e) {
  84. echo $e->getMessage(); exit(1);
  85. }
  86. }
  87.  
  88. function create($table, $data) {
  89. $fields = $this->filter($table, $data);
  90.  
  91. $sql = "INSERT INTO " . $table . " (" . implode($fields, ", ") . ") VALUES (:" . implode($fields, ", :") . ");";
  92.  
  93. $bind = array();
  94. foreach($fields as $field)
  95. $bind[":$field"] = $data[$field];
  96.  
  97. $result = $this->run($sql, $bind);
  98. return $this->db->lastInsertId();
  99. }
  100.  
  101. function read($table, $where="", $bind=array(), $fields="*") {
  102. $sql = "SELECT " . $fields . " FROM " . $table;
  103. if(!empty($where))
  104. $sql .= " WHERE " . $where;
  105. $sql .= ";";
  106.  
  107. $result = $this->run($sql, $bind);
  108. $result->setFetchMode(PDO::FETCH_ASSOC);
  109.  
  110. $rows = array();
  111. while($row = $result->fetch()) {
  112. $rows[] = $row;
  113. }
  114.  
  115. return $rows;
  116. }
  117.  
  118. function update($table, $data, $where, $bind=array()) {
  119. $fields = $this->filter($table, $data);
  120. $fieldSize = sizeof($fields);
  121.  
  122. $sql = "UPDATE " . $table . " SET ";
  123. for($f = 0; $f < $fieldSize; ++$f) {
  124. if($f > 0)
  125. $sql .= ", ";
  126. $sql .= $fields[$f] . " = :update_" . $fields[$f];
  127. }
  128. $sql .= " WHERE " . $where . ";";
  129.  
  130. foreach($fields as $field)
  131. $bind[":update_$field"] = $data[$field];
  132.  
  133. $result = $this->run($sql, $bind);
  134. return $result->rowCount();
  135. }
  136.  
  137. function delete($table, $where, $bind="") {
  138. $sql = "DELETE FROM " . $table . " WHERE " . $where . ";";
  139. $result = $this->run($sql, $bind);
  140. return $result->rowCount();
  141. }
  142.  
  143. private function filter($table, $data) {
  144. $driver = $this->config['dbdriver'];
  145.  
  146. if($driver == 'sqlite') {
  147. $sql = "PRAGMA table_info('" . $table . "');";
  148. $key = "name";
  149. } elseif($driver == 'mysql') {
  150. $sql = "DESCRIBE " . $table . ";";
  151. $key = "Field";
  152. } else {
  153. $sql = "SELECT column_name FROM information_schema.columns WHERE table_name = '" . $table . "';";
  154. $key = "column_name";
  155. }
  156.  
  157. if(false !== ($list = $this->run($sql))) {
  158. $fields = array();
  159. foreach($list as $record)
  160. $fields[] = $record[$key];
  161. return array_values(array_intersect($fields, array_keys($data)));
  162. }
  163.  
  164. return array();
  165. }
  166. }
Add Comment
Please, Sign In to add comment