/other/installable/back_end/dbConnections.php

https://github.com/RishabhJain96/RoboWebProj · PHP · 253 lines · 185 code · 14 blank · 54 comment · 21 complexity · 47369fb5ed2c427ec8a96e85aa902642 MD5 · raw file

  1. <?PHP
  2. class dbConnections
  3. {
  4. //internal properties go below
  5. protected $_dbuser;
  6. protected $_dbpass;
  7. protected $_dbname;
  8. protected $_dbhost;
  9. protected $_conn;
  10. protected $_queryResults;
  11. protected $_connDB;
  12. //constructor goes below
  13. public function __construct($dbname, $dbhost, $dbuser, $dbpass = null) {
  14. try {
  15. $this->_dbuser = $dbuser;
  16. $this->_dbname = $dbname;
  17. $this->_dbhost = $dbhost;
  18. if(!is_null($dbpass)) {
  19. $this->_dbpass = $dbpass;
  20. }
  21. } catch(Exception $err) {
  22. throw new Exception('Could not construct the object');
  23. echo $err;
  24. }
  25. }
  26. //General Methods Below for the dbConnections class
  27. public function open_db_connection()
  28. {
  29. try{
  30. $this->_conn = mysql_connect($this->_dbhost, $this->_dbuser, $this->_dbpass);
  31. $this->_connDB = mysql_select_db($this->_dbname);
  32. return $this->_connDB;
  33. }
  34. catch(Exception $err) {
  35. throw new Exception("The script failed to connect to the database $this->_dbname at $this->_dbhost");
  36. }
  37. }
  38. /**
  39. * Not working as of 24th June, 2011
  40. */
  41. public function close_db_connection()
  42. {
  43. mysql_close($this->_conn);
  44. }
  45. //returns the result of the SELECT query;
  46. public function selectFromTable($tableName, $key = null, $value = null)
  47. {
  48. try{
  49. if(!is_null($key)) {
  50. if(!is_null($value)) {
  51. $result = mysql_query("SELECT * FROM `$tableName` WHERE `$key`='$value'");
  52. return $result;
  53. }
  54. } else {
  55. $result = mysql_query("SELECT * FROM $tableName");
  56. return $result;
  57. }
  58. }
  59. catch(Exception $err) {
  60. throw new Exception('Error: Could not connect execute the SELECT * FROM query as specified in selectFromTable() function!');
  61. }
  62. }
  63. // returns the mysql array in descending order of the column $orderColumn
  64. // author: wilbur yang
  65. public function selectFromTableDesc($tableName, $key = null, $value = null, $orderColumn)
  66. {
  67. try
  68. {
  69. if(!is_null($key))
  70. {
  71. if(!is_null($value))
  72. {
  73. $result = mysql_query("SELECT * FROM `$tableName` WHERE `$key`='$value' ORDER BY `$orderColumn` DESC");
  74. return $result;
  75. }
  76. }
  77. else
  78. {
  79. $result = mysql_query("SELECT * FROM `$tableName` ORDER BY `$orderColumn` DESC");
  80. return $result;
  81. }
  82. }
  83. catch(Exception $err)
  84. {
  85. throw new Exception('Error: Could not connect execute the SELECT * FROM query as specified in selectFromTable() function!');
  86. }
  87. }
  88. // returns the mysql array in ascending order of the colum $orderColumn
  89. // author: wilbur yang
  90. public function selectFromTableAsc($tableName, $key = null, $value = null, $orderColumn)
  91. {
  92. try
  93. {
  94. if(!is_null($key))
  95. {
  96. if(!is_null($value))
  97. {
  98. $result = mysql_query("SELECT * FROM `$tableName` WHERE `$key`='$value' ORDER BY `$orderColumn` ASC");
  99. return $result;
  100. }
  101. }
  102. else
  103. {
  104. $result = mysql_query("SELECT * FROM `$tableName` ORDER BY `$orderColumn` ASC");
  105. return $result;
  106. }
  107. }
  108. catch(Exception $err)
  109. {
  110. throw new Exception('Error: Could not connect execute the SELECT * FROM query as specified in selectFromTable() function!');
  111. }
  112. }
  113. //returns the result of the Insert query
  114. // array_fieldValues is the array of array('fieldId' => 'fieldValue');
  115. public function insertIntoTable($tableName, $array_fieldValues)
  116. {
  117. //change below testing code
  118. // $result1 = mysql_query("INSERT INTO test (test) VALUES ('ll')");
  119. // return $result1;
  120. //end of testing block
  121. $array_values = '';
  122. $array = '';
  123. $count = 1;
  124. if(is_array($array_fieldValues)) {
  125. //iterate through the array for all the values
  126. foreach($array_fieldValues as $key => $value) {
  127. if($count < count($array_fieldValues)) {
  128. $array_values = "$array_values '$value',";
  129. $array = "$array `$key`,";
  130. $count = $count + 1;
  131. } else {
  132. $array_values = trim("$array_values '$value'");
  133. $array = trim("$array `$key`");
  134. }
  135. }
  136. //return $array_values;
  137. //print_r($array);
  138. try {
  139. //testing code below
  140. //return "INSERT INTO $tableName ($array) VALUES ($array_values)";
  141. //testing block end
  142. $result = mysql_query("INSERT INTO $tableName ($array) VALUES ($array_values)");
  143. //change made
  144. //print_r("INSERT INTO $tableName ($array) VALUES ($array_values)");
  145. return $result;
  146. } catch(Exception $err) {
  147. throw new Exception("Error occurred while inserting $array_values into $array in $tableName");
  148. }
  149. } else {
  150. throw new Exception('function insertIntoTable must receive arrays for the last parameter');
  151. }
  152. }
  153. //returns the result of the UPDATE Mysql query
  154. // param: $fieldIds has to be an array
  155. // the fieldIds array should have its corresponding fieldValue mapped to the fieldId it corresponds to;
  156. // e.g. array("field1" => "value1", "field2" => "value2")
  157. //condition should be in the form of 'FieldName = some FieldValue'
  158. public function updateTable($tableName, $fieldIds, $condition)
  159. {
  160. $array_values = '';
  161. $count = 1;
  162. if(is_array($fieldIds)) {
  163. //iterate through the array for all the values
  164. foreach($fieldIds as $key => $value) {
  165. if($count < count($fieldIds)) {
  166. $array_values = "$array_values $key='$value',";
  167. $count = $count + 1;
  168. } else {
  169. $array_values = "$array_values $key='$value'";
  170. }
  171. }
  172. try{
  173. $result = mysql_query("UPDATE $tableName SET $array_values WHERE $condition");
  174. //if($result) print '4';
  175. //print "UPDATE $tableName SET $array_values WHERE $condition";
  176. //Test code below
  177. //$result = "UPDATE $tableName SET $array_values WHERE $condition";
  178. return $result;
  179. } catch(Exception $err) {
  180. throw new Exception("Error occurred while updating $array_values into $tableName");
  181. }
  182. } else {
  183. throw new Exception('the updateTable only takes arrays for the middle parameters');
  184. }
  185. }
  186. /**
  187. * Used to format the queryResults into a readable Array rather than a resource ID.
  188. * @param: $value is the resource
  189. * @param: $field (optional) is the field (or column) from the data set you would like to extract.
  190. */
  191. public function formatQueryResults($value, $field = null)
  192. {
  193. $array = array();
  194. $i = 0;
  195. if(!is_null($field)) {
  196. while($rows = mysql_fetch_array($value)) {
  197. $array[$i] = $rows[$field];
  198. $i++;
  199. }
  200. return $array;
  201. } else {
  202. $rows = mysql_fetch_array($value);
  203. return $rows;
  204. }
  205. }
  206. /**
  207. * Public method selectFromTableMultiple
  208. * Will select from the Database table the entries that fit the various filters ($key1, $key2 and their respective values)
  209. * Only the entries that satisfy both requirements will be passed through;
  210. */
  211. public function selectFromTableMultiple($tableName, $key1, $value1, $key2, $value2)
  212. {
  213. $result = mysql_query("SELECT * FROM $tableName WHERE $key1='$value1' AND $key2='$value2'");
  214. return $result;
  215. }
  216. /**
  217. * description: Check if the given table is empty.
  218. *
  219. * @param table: The table to check emptiness of.
  220. * @return bool: true if empty, false otherwise
  221. */
  222. public function tableIsEmpty($table)
  223. {
  224. //print 'checking table empty';
  225. $sql = "SELECT * FROM $table";
  226. $result = @mysql_query($sql);
  227. print_r($result);
  228. if (!$result)
  229. {
  230. //print 'table empty';
  231. return true;
  232. }
  233. else
  234. {
  235. //print 'table not empty';
  236. return false;
  237. }
  238. }
  239. }