/library/SQLQuery.class.php

https://github.com/indieenergy/thresholds-php · PHP · 251 lines · 192 code · 47 blank · 12 comment · 39 complexity · 2f8f69b4f59eec97da5c2c487be97602 MD5 · raw file

  1. <?php
  2. class SQLQuery {
  3. protected $_dbHandle;
  4. protected $_result;
  5. protected $_query;
  6. protected $_table;
  7. protected $_describe = array();
  8. protected $_orderBy;
  9. protected $_order;
  10. protected $_extraConditions;
  11. protected $_limit;
  12. /** Connects to database **/
  13. function connect($address, $account, $pwd, $name) {
  14. $this->_dbHandle = @mysql_connect($address, $account, $pwd);
  15. if ($this->_dbHandle != 0) {
  16. if (mysql_select_db($name, $this->_dbHandle)) {
  17. return 1;
  18. }
  19. else {
  20. return 0;
  21. }
  22. }
  23. else {
  24. return 0;
  25. }
  26. }
  27. /** Disconnects from database **/
  28. function disconnect() {
  29. if (@mysql_close($this->_dbHandle) != 0) {
  30. return 1;
  31. } else {
  32. return 0;
  33. }
  34. }
  35. /** Select Query **/
  36. function where($field, $value) {
  37. $this->_extraConditions .= '`'.$this->_model.'`.`'.$field.'` = \''.mysql_real_escape_string($value).'\' AND ';
  38. }
  39. function like($field, $value) {
  40. $this->_extraConditions .= '`'.$this->_model.'`.`'.$field.'` LIKE \'%'.mysql_real_escape_string($value).'%\' AND ';
  41. }
  42. function orderBy($orderBy, $order = 'ASC') {
  43. $this->_orderBy = $orderBy;
  44. $this->_order = $order;
  45. }
  46. function search() {
  47. $from = '`'.$this->_table.'` as `'.$this->_model.'` ';
  48. $conditions = '\'1\'=\'1\' AND ';
  49. if ($this->id) {
  50. $conditions .= '`'.$this->_model.'`.`id` = \''.mysql_real_escape_string($this->id).'\' AND ';
  51. }
  52. if ($this->_extraConditions) {
  53. $conditions .= $this->_extraConditions;
  54. }
  55. $conditions = substr($conditions,0,-4);
  56. if (isset($this->_orderBy)) {
  57. $conditions .= ' ORDER BY `'.$this->_model.'`.`'.$this->_orderBy.'` '.$this->_order;
  58. }
  59. if (isset($this->_page)) {
  60. $offset = ($this->_page-1)*$this->_limit;
  61. $conditions .= ' LIMIT '.$this->_limit.' OFFSET '.$offset;
  62. }
  63. $this->_query = 'SELECT * FROM '.$from.' WHERE '.$conditions;
  64. $this->_result = mysql_query($this->_query, $this->_dbHandle);
  65. $result = array();
  66. $table = array();
  67. $field = array();
  68. $tempResults = array();
  69. $numOfFields = mysql_num_fields($this->_result);
  70. for ($i = 0; $i < $numOfFields; ++$i) {
  71. array_push($table,mysql_field_table($this->_result, $i));
  72. array_push($field,mysql_field_name($this->_result, $i));
  73. }
  74. if (mysql_num_rows($this->_result) > 0 ) {
  75. while ($row = mysql_fetch_row($this->_result)) {
  76. for ($i = 0;$i < $numOfFields; ++$i) {
  77. $tempResults[$table[$i]][$field[$i]] = $row[$i];
  78. }
  79. array_push($result,$tempResults);
  80. }
  81. if (mysql_num_rows($this->_result) == 1 && $this->id != null) {
  82. mysql_free_result($this->_result);
  83. $this->clear();
  84. return($result[0]);
  85. }
  86. else {
  87. mysql_free_result($this->_result);
  88. $this->clear();
  89. return($result);
  90. }
  91. }
  92. else {
  93. mysql_free_result($this->_result);
  94. $this->clear();
  95. return $result;
  96. }
  97. }
  98. /** Custom SQL Query **/
  99. function custom($query) {
  100. $this->_result = mysql_query($query, $this->_dbHandle);
  101. $result = array();
  102. $table = array();
  103. $field = array();
  104. $tempResults = array();
  105. if(substr_count(strtoupper($query),"SELECT")>0) {
  106. if (mysql_num_rows($this->_result) > 0) {
  107. $numOfFields = mysql_num_fields($this->_result);
  108. for ($i = 0; $i < $numOfFields; ++$i) {
  109. array_push($table,mysql_field_table($this->_result, $i));
  110. array_push($field,mysql_field_name($this->_result, $i));
  111. }
  112. while ($row = mysql_fetch_row($this->_result)) {
  113. for ($i = 0;$i < $numOfFields; ++$i) {
  114. $table[$i] = ucfirst($table[$i]);
  115. $tempResults[$table[$i]][$field[$i]] = $row[$i];
  116. }
  117. array_push($result,$tempResults);
  118. }
  119. }
  120. mysql_free_result($this->_result);
  121. }
  122. $this->clear();
  123. return($result);
  124. }
  125. /** Describes a Table **/
  126. protected function _describe() {
  127. $this->_describe = null;
  128. if (!$this->_describe) {
  129. $this->_describe = array();
  130. $query = 'DESCRIBE '.$this->_table;
  131. $this->_result = mysql_query($query, $this->_dbHandle);
  132. while ($row = mysql_fetch_row($this->_result)) {
  133. array_push($this->_describe,$row[0]);
  134. }
  135. mysql_free_result($this->_result);
  136. }
  137. foreach ($this->_describe as $field) {
  138. $this->$field = null;
  139. }
  140. }
  141. /** Delete an Object **/
  142. function delete() {
  143. if ($this->id) {
  144. $query = 'DELETE FROM '.$this->_table.' WHERE `id`=\''.mysql_real_escape_string($this->id).'\'';
  145. $this->_result = mysql_query($query, $this->_dbHandle);
  146. $this->clear();
  147. if ($this->_result == 0) {
  148. /** Error Generation **/
  149. return -1;
  150. }
  151. }
  152. else {
  153. /** Error Generation **/
  154. return -1;
  155. }
  156. }
  157. /** Saves an Object i.e. Updates/Inserts Query **/
  158. function save() {
  159. $query = '';
  160. if (isset($this->id)) {
  161. $updates = '';
  162. foreach ($this->_describe as $field) {
  163. if ($this->$field) {
  164. $updates .= '`'.$field.'` = \''.mysql_real_escape_string($this->$field).'\',';
  165. }
  166. }
  167. $updates = substr($updates,0,-1);
  168. $query = 'UPDATE '.$this->_table.' SET '.$updates.' WHERE `id`=\''.mysql_real_escape_string($this->id).'\'';
  169. }
  170. else {
  171. $fields = '';
  172. $values = '';
  173. foreach ($this->_describe as $field) {
  174. if ($this->$field) {
  175. $fields .= '`'.$field.'`,';
  176. $values .= '\''.mysql_real_escape_string($this->$field).'\',';
  177. }
  178. }
  179. $values = substr($values,0,-1);
  180. $fields = substr($fields,0,-1);
  181. $query = 'INSERT INTO '.$this->_table.' ('.$fields.') VALUES ('.$values.')';
  182. }
  183. $this->_result = mysql_query($query, $this->_dbHandle);
  184. $this->clear();
  185. if ($this->_result == 0) {
  186. /** Error Generation **/
  187. return -1;
  188. }
  189. }
  190. /** Clear All Variables **/
  191. function clear() {
  192. foreach($this->_describe as $field) {
  193. $this->$field = null;
  194. }
  195. $this->_orderby = null;
  196. $this->_extraConditions = null;
  197. $this->_page = null;
  198. $this->_order = null;
  199. }
  200. /** Get error string **/
  201. function getError() {
  202. return mysql_error($this->_dbHandle);
  203. }
  204. }