Advertisement
Guest User

Untitled

a guest
Jul 18th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.59 KB | None | 0 0
  1. <?php
  2.  
  3. class DB
  4. {
  5.  
  6. protected $db_host = 'localhost';
  7. protected $db_user = 'root';
  8. protected $db_pass = '';
  9. protected $db_name = 'db_loan';
  10.  
  11.  
  12. private $con = false;
  13. public $result = array();
  14.  
  15.  
  16. function connect()
  17. {
  18. if(!$this->con)
  19. {
  20. $myconn = @mysql_connect($this->db_host,$this->db_user,$this->db_pass);
  21. if($myconn)
  22. {
  23. $seldb = @mysql_select_db($this->db_name,$myconn);
  24. if($seldb)
  25. {
  26. $this->con = true;
  27. return true;
  28. }
  29. else
  30. {
  31. return false;
  32. }
  33. }
  34. else
  35. {
  36. return false;
  37. }
  38. }
  39. else
  40. {
  41. return true;
  42. }
  43. }
  44.  
  45.  
  46. function setDatabase($name)
  47. {
  48. if($this->con)
  49. {
  50. if(@mysql_close())
  51. {
  52. $this->con = false;
  53. $this->results = null;
  54. $this->db_name = $name;
  55. $this->connect();
  56. }
  57. }
  58.  
  59. }
  60.  
  61.  
  62. function tableExists($table)
  63. {
  64. $tablesInDb = @mysql_query('SHOW TABLES FROM '.$this->db_name.' LIKE "'.$table.'"');
  65. if($tablesInDb)
  66. {
  67. if(mysql_num_rows($tablesInDb)==1)
  68. {
  69. return true;
  70. }
  71. else
  72. {
  73. return false;
  74. }
  75. }
  76. }
  77.  
  78.  
  79. function select($table, $rows = '*', $where = null, $group = null, $order = null, $limit = null)
  80. {
  81. $q = 'SELECT '.$rows.' FROM '.$table;
  82. /*if($where != null)
  83. $q .= ' WHERE '.$where;
  84. if($order != null)
  85. $q .= ' ORDER BY '.$order;
  86. */
  87. if (!empty($where)) {
  88. $q .= " WHERE $where";
  89. }
  90.  
  91. if ($group !== null) {
  92. $q .= " GROUP BY $group";
  93. }
  94. if ($order !== null) {
  95. $q .= " ORDER BY $order";
  96. }
  97. if ($limit !== null && is_array($limit)) {
  98. $start = (int) $limit['start'];
  99. $end = (int) $limit['end'];
  100. $q .= " LIMIT $start, $end";
  101. }
  102.  
  103. $query = @mysql_query($q);
  104. if($query)
  105. {
  106. $this->numResults = mysql_num_rows($query);
  107. for($i = 0; $i < $this->numResults; $i++)
  108. {
  109. $r = mysql_fetch_array($query);
  110. $key = array_keys($r);
  111. for($x = 0; $x < count($key); $x++)
  112. {
  113. // Sanitizes keys so only alphavalues are allowed
  114. if(!is_int($key[$x]))
  115. {
  116. if(mysql_num_rows($query) > 1)
  117. $this->result[$i][$key[$x]] = $r[$key[$x]];
  118. else if(mysql_num_rows($query) < 1)
  119. $this->result = null;
  120. else
  121. $this->result[$key[$x]] = $r[$key[$x]];
  122. }
  123. }
  124. }
  125. return true;
  126. }
  127. else
  128. {
  129. return false;
  130. }
  131. }
  132.  
  133.  
  134.  
  135.  
  136. function insert($table,$values,$rows = null)
  137. {
  138. if($this->tableExists($table))
  139. {
  140. $insert = 'INSERT INTO '.$table;
  141. if($rows != null)
  142. {
  143. $insert .= ' ('.$rows.')';
  144. }
  145.  
  146. for($i = 0; $i < count($values); $i++)
  147. {
  148. if(is_string($values[$i]))
  149. $values[$i] = '"'.$values[$i].'"';
  150. }
  151. $values = implode(',',$values);
  152. $insert .= ' VALUES ('.$values.')';
  153.  
  154. $ins = @mysql_query($insert);
  155.  
  156. if($ins)
  157. {
  158. return true;
  159.  
  160. }
  161. else
  162. {
  163. return false;
  164. }
  165. }
  166. }
  167.  
  168.  
  169. function delete($table,$where = null)
  170. {
  171. if($this->tableExists($table))
  172. {
  173. if($where == null)
  174. {
  175. $delete = 'DELETE '.$table;
  176. }
  177. else
  178. {
  179. $delete = 'DELETE FROM '.$table.' WHERE '.$where;
  180. }
  181. $del = @mysql_query($delete);
  182.  
  183. if($del)
  184. {
  185. return true;
  186. }
  187. else
  188. {
  189. return false;
  190. }
  191. }
  192. else
  193. {
  194. return false;
  195. }
  196. }
  197.  
  198.  
  199. function update($table,$rows,$where)
  200. {
  201. if($this->tableExists($table))
  202. {
  203.  
  204. for($i = 0; $i < count($where); $i++)
  205. {
  206. if($i%2 != 0)
  207. {
  208. if(is_string($where[$i]))
  209. {
  210. if(($i+1) != null)
  211. $where[$i] = '"'.$where[$i].'" AND ';
  212. else
  213. $where[$i] = '"'.$where[$i].'"';
  214. }
  215. }
  216. }
  217. $where = implode('',$where);
  218.  
  219.  
  220. $update = 'UPDATE '.$table.' SET ';
  221. $keys = array_keys($rows);
  222. for($i = 0; $i < count($rows); $i++)
  223. {
  224. if(is_string($rows[$keys[$i]]))
  225. {
  226. $update .= $keys[$i].'="'.$rows[$keys[$i]].'"';
  227. }
  228. else
  229. {
  230. $update .= $keys[$i].'='.$rows[$keys[$i]];
  231. }
  232.  
  233. if($i != count($rows)-1)
  234. {
  235. $update .= ',';
  236. }
  237. }
  238. $update .= ' WHERE '.$where;
  239. $query = @mysql_query($update);
  240. if($query)
  241. {
  242. return true;
  243. }
  244. else
  245. {
  246. return false;
  247. }
  248. }
  249. else
  250. {
  251. return false;
  252. }
  253. }
  254.  
  255.  
  256. public function getResult()
  257. {
  258. return $this->result;
  259. }
  260.  
  261.  
  262. function auth($form) {
  263.  
  264. $username = $form['username'];
  265. $password = $form['password'];
  266.  
  267. $query = mysql_query("SELECT * FROM user WHERE username='$username' and password = md5('$password')");
  268. $countResult = mysql_num_rows($query);
  269. if($countResult>0)
  270. {
  271. while($row = mysql_fetch_array($query))
  272. {
  273. $_SESSION['User_Level'] = $row['userlevel'];
  274. $_SESSION['id'] = $row['id'];
  275. $_SESSION['name'] = $row['username'];
  276. }
  277. if($_SESSION['User_Level'] == 1)
  278. {
  279. echo "<meta http-equiv='refresh' content=0;URL=addmember.php>";
  280. exit;
  281. }
  282. }
  283. }
  284.  
  285.  
  286. function session() {
  287.  
  288. ob_start();
  289. session_start();
  290. if(!(isset($_SESSION['name'])))
  291. header("Location:index.php");
  292.  
  293. }
  294.  
  295.  
  296. function logout() {
  297.  
  298. session_destroy();
  299. header("location:index.php");
  300.  
  301. }
  302.  
  303.  
  304.  
  305. }
  306.  
  307.  
  308. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement