mysqli db_wrapper


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. class Db {
  2. private $link;
  3.  
  4. // Constructor method.
  5. // -------------------------------------------------
  6. function Db($host, $username, $password, $database) {
  7. $this->connect($host, $username, $password, $database);
  8. }
  9.  
  10. function connect($host, $username, $password, $database) {
  11. $this->link = mysqli_connect($host,$username,$password,$database);
  12. $this->dbprefix = $dbprefix;
  13.  
  14. if (!$this->link)
  15. exit('Connect failed: '.mysqli_connect_error());
  16.  
  17. if (!mysqli_set_charset($this->link,'utf8'))
  18. exit('Error loading character set utf8: '.mysqli_error($this->link));
  19. }
  20.  
  21. // Delete method.
  22. // -------------------------------------------------
  23. function delete($table, $id) {
  24. $sql = "DELETE FROM `" . $table . "` WHERE `id` = '" . Db::escape($id) . "'";
  25. return mysqli_query($this->link,$sql);
  26. }
  27.  
  28. // Select method.
  29. // -------------------------------------------------
  30. function select($table, $id=null) {
  31. // No $id given, thus select all rows.
  32. if ( $id!="0" && empty($id)) {
  33. $sql = "SELECT * FROM `" . $table . "` ORDER BY `id`";
  34. }
  35.  
  36. // grabs rows matching where clauses given
  37. elseif (is_array($id)) {
  38. $sql = "SELECT * FROM `" . $table . "` WHERE ";
  39. $first=true;
  40. foreach($id as $col=>$val) {
  41. if($first) $first=false;
  42. else $sql.=" AND ";
  43. $sql .= "`".Db::escape($col)."` = '" . Db::escape($val) . "'";
  44. }
  45.  
  46. // Grabs the row associated with the given $id.
  47. } else {
  48. $sql = "SELECT * FROM `" . $table . "` WHERE `id` = '" . Db::escape($id) . "'";
  49. }
  50.  
  51. return mysqli_query($this->link,$sql);
  52. }
  53.  
  54. // Update method.
  55. // -------------------------------------------------
  56. function update($table, $id) {
  57. $getColumns = mysqli_query($this->link,"SELECT * FROM " . $table);
  58. while($column = mysqli_fetch_field($getColumns)) {
  59. $column = $column->name;
  60. if (isset($_POST[$column])) {
  61. Utils::manipulateValues($column); // Manipulate certain values before inserting them into db.
  62. // This will be built up-on in the future.
  63.  
  64. $fields[] = "`" . $column . "` = '" . htmlspecialchars($_POST[$column]) . "'";
  65. }
  66. }
  67.  
  68. $sql = "UPDATE `" . $table . "` SET " . implode(", ", $fields) . " WHERE `id` = '" . $id . "'";
  69. return mysqli_query($this->link,$sql);
  70. }
  71.  
  72. // Insert method.
  73. // -------------------------------------------------
  74. function insert($table) {
  75. $getColumns = mysqli_query($this->link,"SELECT * FROM " . $table);
  76.  
  77. while($column = mysqli_fetch_field($getColumns)) {
  78. $column = $column->name;
  79. if (isset($_POST[$column])) {
  80. Utils::manipulateValues($column); // Manipulate certain values before inserting them into db.
  81. // This will be built up-on in the future.
  82.  
  83. $fields[$column] = "'" . htmlspecialchars($_POST[$column]) . "'";
  84. }
  85. }
  86.  
  87. $sql = "INSERT INTO `" . $table . "` (`" . implode("`, `", array_keys($fields)) . "`) VALUES (" . implode(", ", $fields) . ")";
  88. mysqli_query($this->link,$sql);
  89. return mysqli_insert_id($this->link);
  90. }
  91.  
  92. // Normal query for custom needs.
  93. // NOTICE: When using this method, it is your job to assure user submitted-data is secure.
  94. // -------------------------------------------------
  95. function query($sql) {
  96. return mysqli_query($this->link,$sql);
  97. }
  98.  
  99. function num_fields($result) {
  100. return mysqli_num_fields($result);
  101. }
  102. function fetch_field($result) {
  103. return mysqli_fetch_field($result);
  104. }
  105.  
  106. function fetch_row($result) {
  107. return mysqli_fetch_row($result);
  108. }
  109.  
  110. function num_rows($result) {
  111. return mysqli_num_rows($result);
  112. }
  113.  
  114. function fetch_array($result) {
  115. return mysqli_fetch_array($result);
  116. }
  117. function fetch_assoc($result) {
  118. return mysqli_fetch_assoc($result);
  119. }
  120. function escape($string) {
  121. return mysqli_real_escape_string($this->link,$string);
  122. }
  123.  
  124. // Check for tables existance.
  125. function table_exists($sector) {
  126. $getTables = mysqli_query($this->link,"SHOW TABLES");
  127. while($table = mysqli_fetch_array($getTables)) {
  128. if ($sector == $table[0]) {
  129. return true;
  130. }
  131. }
  132. }
  133.  
  134. function show_columns($table, $column) {
  135. return mysqli_query($this->link,"SHOW COLUMNS FROM `" . $table . "` LIKE '" . $column . "'");
  136. }
  137.  
  138. }
  139.  
  140. $Db = new Db($settings['database']['databaseHost'], $settings['database']['databaseUsername'], $settings['database']['databasePassword'], $settings['database']['databaseName']);

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.