Guest User

Untitled

a guest
Jul 19th, 2018
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.28 KB | None | 0 0
  1. <?php
  2. /* By Sam Jesso - sam@bike-this.com
  3. * File: oceansql.php
  4. * License: GNU GPL.
  5. * Type: PHP
  6. * Description: A MySQL (PHP 4 mysql_*) wrapper that makes queries visually easier to follow, execute, and debug. */
  7.  
  8. defined("SYSTEM") or die("Access denied.");
  9. class MySQL
  10. {
  11. private $query, $queryData, $whereCount = 0;
  12. public $numQueries;
  13. public function __construct($user, $pass, $db, $host = "localhost")
  14. {
  15. $conn = mysql_connect($host, $user, $pass) or die(mysql_error());
  16. mysql_select_db($db, $conn) or die(mysql_error());
  17. return $this;
  18. }
  19. public function __destruct()
  20. {
  21. mysql_close();
  22. }
  23. public function __toString()
  24. {
  25. return (string) $this->numQueries;
  26. }
  27. public function select($from, $fields = array())
  28. {
  29. if(count($fields) > 0)
  30. {
  31. $fieldstr = "";
  32. foreach($fields as $key => $value)
  33. $fieldstr.= (is_numeric($key)) ? ", {$value}" : ", {$key} AS {$value}";
  34.  
  35. $fieldstr = substr($fieldstr, 2);
  36. }
  37. else
  38. {
  39. $fieldstr = "*";
  40. }
  41. $this->query.= "SELECT {$fieldstr} FROM {$from} ";
  42. return $this;
  43. }
  44. public function query($query)
  45. {
  46. $this->queryData = mysql_query($query) or die(mysql_error());
  47. $this->numQueries++;
  48. return $this;
  49. }
  50. public function rawquery($query)
  51. {
  52. $this->numQueries++;
  53. return mysql_query($query);
  54. }
  55. public function update($table)
  56. {
  57. $this->query.= "UPDATE {$table} ";
  58. return $this;
  59. }
  60. public function set($settings = array())
  61. {
  62. $this->query.= "SET ";
  63. $set = "";
  64. foreach($settings as $key => $value)
  65. $set.= ", {$key} = '{$value}'";
  66.  
  67. $set = substr($set, 2);
  68. $this->query.= $set." ";
  69. return $this;
  70. }
  71. public function delete($from)
  72. {
  73. $this->query.= "DELETE FROM {$from} ";
  74. return $this;
  75. }
  76. public function insert($into)
  77. {
  78. $this->query.= "INSERT INTO {$into} ";
  79. return $this;
  80. }
  81. public function values($array)
  82. {
  83. $keys = "";
  84. $values = "";
  85. foreach($array as $key => $value)
  86. {
  87. $keys.= ", {$key}";
  88. $values.= ", '{$value}'";
  89. }
  90. $keys = substr($keys, 2);
  91. $values = substr($values, 2);
  92. $this->query.= "({$keys}) VALUES ({$values}) ";
  93. return $this;
  94. }
  95. public function join($table, $on = array(), $type = "AND")
  96. {
  97. $this->query.= "INNER JOIN {$table} ";
  98. if(count($on) > 0)
  99. {
  100. $where = "";
  101. $this->query.= "ON ";
  102. foreach($on as $key => $value)
  103. {
  104. $where.= " {$type} {$key} = {$value}";
  105. }
  106. $where = ($type == "AND") ? substr($where, 5)." " : substr($where, 4)." ";
  107. $this->query.= $where;
  108. }
  109. return $this;
  110. }
  111. public function where($args = array(), $type = "AND")
  112. {
  113. $where = "";
  114. foreach($args as $key => $value)
  115. {
  116. $where.= ($this->whereCount == 0) ? "WHERE {$key} = '{$value}' " : "{$type} {$key} = '{$value}' ";
  117. $this->whereCount++;
  118. }
  119. $this->query.= $where;
  120. return $this;
  121. }
  122. public function search($args = array(), $type = "AND")
  123. {
  124. $where = "";
  125. foreach($args as $key => $value)
  126. {
  127. $where.= ($this->whereCount == 0) ? "WHERE {$key} LIKE '%{$value}%' " : "{$type} {$key} LIKE '%{$value}%' ";
  128. $this->whereCount++;
  129. }
  130. $this->query.= $where;
  131. return $this;
  132. }
  133. public function order($by, $type = "DESC")
  134. {
  135. if(strtoupper($by) == "RAND")
  136. {
  137. $type = "";
  138. $by = "RAND()";
  139. }
  140. else
  141. {
  142. $type = ($type == "DESC") ? " DESC" : " ASC";
  143. }
  144. $this->query.= "ORDER BY {$by}{$type} ";
  145. return $this;
  146. }
  147. public function limit($start, $finish = NULL)
  148. {
  149. $limit = "LIMIT {$start} ";
  150. if($finish)
  151. $limit.= substr($limit, 0, -1).",{$finish} ";
  152.  
  153. $this->query.= $limit;
  154. return $this;
  155. }
  156. public function execute()
  157. {
  158. $this->numQueries++;
  159. $this->whereCount = 0;
  160. $this->query = substr($this->query, 0, -1);
  161. $this->queryData = mysql_query($this->query) or die(mysql_error());
  162. $this->query = "";
  163. return $this;
  164. }
  165. public function fetch($as = "object")
  166. {
  167. return ($as == "object") ? mysql_fetch_object($this->queryData) : mysql_fetch_array($this->queryData);
  168. }
  169. public function results()
  170. {
  171. return mysql_num_rows($this->queryData);
  172. }
  173. public function debug($html = true) {
  174. $info = array(
  175. "Number of queries executed" => $this->numQueries,
  176. "Current query in queue" => $this->query
  177. );
  178. if($html)
  179. {
  180. foreach($info as $key => $value)
  181. echo "<strong>$key:</strong> $value<br />";
  182. }
  183. else
  184. {
  185. print_r($info);
  186. }
  187. }
  188. public function clear()
  189. {
  190. $this->query = "";
  191. $this->queryData = "";
  192. $this->whereCount = 0;
  193. return true;
  194. }
  195. }
Add Comment
Please, Sign In to add comment