Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. <?php
  2. class Dbase {
  3.  
  4. private $_host = "localhost";
  5. private $_user = "root";
  6. private $_password = "";
  7. private $_name = "7afrik";
  8.  
  9. private $_conndb = false;
  10. public $_last_query = null;
  11. public $_affected_rows = 0;
  12.  
  13. public $_insert_keys = array();
  14. public $_insert_values = array();
  15. public $_update_sets = array();
  16.  
  17. public $_id;
  18.  
  19.  
  20. public function __construct() {
  21. $this->connect();
  22. }
  23.  
  24. private function connect() {
  25. $this->_conndb = mysqli_connect($this->_host, $this->_user, $this->_password);
  26.  
  27. if (!$this->_conndb) {
  28. die("Database connection failed:<br />" . mysqli_errno());
  29. } else {
  30. $_select = mysqli_select_db($this->_name, $this->_conndb);
  31. if (!$_select) {
  32. die("Database selection failed:<br />" . mysqli_errno());
  33. }
  34. }
  35. mysqli_set_charset("utf8", $this->_conndb);
  36. }
  37.  
  38.  
  39. public function close() {
  40. if (!mysql_close($this->_conndb)) {
  41. die("Closing connection failed.");
  42. }
  43. }
  44.  
  45.  
  46. public function escape($value) {
  47. if(function_exists("mysql_real_escape_string")) {
  48. if (get_magic_quotes_gpc()) {
  49. $value = stripslashes($value);
  50. }
  51. $value = mysql_real_escape_string($value);
  52. } else {
  53. if(!get_magic_quotes_gpc()) {
  54. $value = addcslashes($value);
  55. }
  56. }
  57. return $value;
  58. }
  59.  
  60.  
  61. public function query($sql) {
  62. $this->_last_query = $sql;
  63. $result = mysql_query($sql, $this->_conndb);
  64. $this->displayQuery($result);
  65. return $result;
  66. }
  67.  
  68.  
  69. public function displayQuery($result) {
  70. if(!$result) {
  71. $output = "Database query failed: ". mysql_error() . "<br />";
  72. $output .= "Last SQL query was: ".$this->_last_query;
  73. die($output);
  74. } else {
  75. $this->_affected_rows = mysql_affected_rows($this->_conndb);
  76. }
  77. }
  78.  
  79.  
  80. public function fetchAll($sql) {
  81. $result = $this->query($sql);
  82. $out = array();
  83. while($row = mysql_fetch_assoc($result)) {
  84. $out[] = $row;
  85. }
  86. mysql_free_result($result);
  87. return $out;
  88. }
  89.  
  90. public function fetchOne($sql) {
  91. $out = $this->fetchAll($sql);
  92. return array_shift($out);
  93. }
  94.  
  95. public function lastId() {
  96. return mysql_insert_id($this->_conndb);
  97. }
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107. public function prepareInsert($array = null) {
  108. if (!empty($array)) {
  109. foreach($array as $key => $value) {
  110. $this->_insert_keys[] = $key;
  111. $this->_insert_values[] = $this->escape($value);
  112. }
  113. }
  114. }
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123. public function insert($table = null) {
  124.  
  125. if (
  126. !empty($table) &&
  127. !empty($this->_insert_keys) &&
  128. !empty($this->_insert_values)
  129. ) {
  130.  
  131. $sql = "INSERT INTO `{$table}` (`";
  132. $sql .= implode("`, `", $this->_insert_keys);
  133. $sql .= "`) VALUES ('";
  134. $sql .= implode("', '", $this->_insert_values);
  135. $sql .= "')";
  136.  
  137. if ($this->query($sql)) {
  138. $this->_id = $this->lastId();
  139. return true;
  140. }
  141. return false;
  142.  
  143. }
  144.  
  145. }
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152. public function prepareUpdate($array = null) {
  153. if (!empty($array)) {
  154. foreach($array as $key => $value) {
  155. $this->_update_sets[] = "`{$key}` = '".$this->escape($value)."'";
  156. }
  157. }
  158. }
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167. public function update($table = null, $id = null) {
  168. if (!empty($table) && !empty($id) && !empty($this->_update_sets)) {
  169. $sql = "UPDATE `{$table}` SET ";
  170. $sql .= implode(", ", $this->_update_sets);
  171. $sql .= " WHERE `id` = '".$this->escape($id)."'";
  172. return $this->query($sql);
  173. }
  174. }
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement