/model/database.php

https://github.com/aaraashkhan/echoback · PHP · 364 lines · 212 code · 77 blank · 75 comment · 26 complexity · 4847ee9f40c047654103a9eedfee1db8 MD5 · raw file

  1. <?php
  2. /**
  3. DataBase Module which is
  4. only resposible for makeing predined queries
  5. and it is not a singelton design
  6. */
  7. class MySqlPredefined{
  8. private $user_name;
  9. private $server;
  10. private $password;
  11. private $database;
  12. public $dbObject;
  13. private $db_handle;
  14. private $db_found;
  15. private $DBH;
  16. /**
  17. Initializing the database
  18. and handling it's connection
  19. */
  20. public function __construct($server,$user_name,$password,$database) {
  21. $this->server = $server;
  22. $this->user_name = $user_name;
  23. $this->password = $password;
  24. $this->database = $database;
  25. $this->DBH = new PDO("mysql:host=$server;dbname=$database", $user_name, $password,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
  26. $this->DBH ->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  27. $this->DBH ->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
  28. }
  29. /**
  30. A general method that returns the whole row
  31. of a defined table
  32. */
  33. public function generalSelectWithNoWhere($table_name){
  34. if ($this->DBH) {
  35. //echo "SELECT * from $table_name WHERE $clause";die();
  36. $STH = $this->DBH->query("SELECT * from $table_name");
  37. $STH->setFetchMode(PDO::FETCH_ASSOC);
  38. // $SQL = "SELECT * FROM $table_name WHERE $clause";
  39. // //echo $SQL;die();
  40. // $result = mysql_query($SQL);
  41. // mysql_close($db_handle);
  42. return $STH;
  43. }
  44. else {
  45. print "Database NOT Found ";
  46. }
  47. }
  48. /**
  49. A general method that returns the whole row
  50. of a defined table with predicator
  51. */
  52. public function generalSelectWithPredicator($table_name,$clause){
  53. if ($this->DBH) {
  54. //echo "SELECT * from $table_name WHERE $clause";die();
  55. $STH = $this->DBH->query("SELECT * from $table_name WHERE $clause");
  56. $STH->setFetchMode(PDO::FETCH_ASSOC);
  57. // $SQL = "SELECT * FROM $table_name WHERE $clause";
  58. //echo $SQL;die();
  59. // $result = mysql_query($SQL);
  60. // mysql_close($db_handle);
  61. return $STH;
  62. }
  63. else {
  64. print "Database NOT Found ";
  65. }
  66. }
  67. /**
  68. A general method that returns the whole row
  69. of a defined table with predicator
  70. */
  71. public function generalSelectWithPredicatorAndOrder($table_name,$clause,$order){
  72. if ($this->DBH) {
  73. //echo "SELECT * FROM ORDER BY $order $table_name WHERE $clause";die();
  74. $STH = $this->DBH->query("SELECT * FROM $table_name WHERE $clause ORDER BY $order");
  75. $STH->setFetchMode(PDO::FETCH_ASSOC);
  76. // $SQL = "SELECT * FROM $table_name WHERE $clause ORDER BY $order";
  77. // echo $SQL;die();
  78. // $result = mysql_query($SQL);
  79. // mysql_close($db_handle);
  80. return $STH;
  81. }
  82. else {
  83. print "Database NOT Found ";
  84. }
  85. }
  86. /**
  87. A general method that returns the count
  88. of field
  89. */
  90. public function generalCountWithPredicator($table_name,$row,$clause){
  91. $db_handle = mysql_connect($this->server,$this->user_name,$this->password);
  92. $db_found = mysql_select_db($this->database, $db_handle);
  93. if ($db_found) {
  94. $SQL = "SELECT COUNT('$row') AS TOTAL FROM $table_name WHERE $clause";
  95. //echo $SQL;
  96. $result = mysql_query($SQL);
  97. mysql_close($db_handle);
  98. return $result;
  99. }
  100. else {
  101. print "Database NOT Found ";
  102. mysql_close($db_handle);
  103. }
  104. }
  105. public function newGeneralCountWithPredicator($table_name,$row,$clause){
  106. if ($this->DBH) {
  107. //echo "SELECT COUNT('$row') AS TOTAL FROM $table_name WHERE $clause";die();
  108. $STH = $this->DBH->query("SELECT COUNT('$row') AS TOTAL FROM $table_name WHERE $clause");
  109. $STH->setFetchMode(PDO::FETCH_ASSOC);
  110. // $SQL = "SELECT * FROM $table_name WHERE $clause ORDER BY $order";
  111. // echo $SQL;die();
  112. // $result = mysql_query($SQL);
  113. // mysql_close($db_handle);
  114. $row = $STH -> fetch();
  115. return $row['TOTAL'];
  116. //die();
  117. }
  118. else {
  119. print "Database NOT Found ";
  120. }
  121. }
  122. /**
  123. A general method that returns the sum
  124. of field
  125. */
  126. public function generalSumWithPredicator($table_name,$row,$clause){
  127. $db_handle = mysql_connect($this->server,$this->user_name,$this->password);
  128. $db_found = mysql_select_db($this->database, $db_handle);
  129. if ($db_found) {
  130. $SQL = "SELECT SUM($row) AS TOTAL FROM $table_name WHERE $clause";
  131. //die($SQL);
  132. $result = mysql_query($SQL);
  133. mysql_close($db_handle);
  134. return $result;
  135. }
  136. else {
  137. print "Database NOT Found ";
  138. mysql_close($db_handle);
  139. }
  140. }
  141. /**
  142. * A general method that returns the rows
  143. * seperated for foreach
  144. */
  145. public function generalSelectWithPredicatorSeperated($table_name,$clause){
  146. $db_handle = mysql_connect($this->server,$this->user_name,$this->password);
  147. $db_found = mysql_select_db($this->database, $db_handle);
  148. if ($db_found) {
  149. $SQL = "SELECT * FROM $table_name WHERE $clause";
  150. $result = mysql_query($SQL);
  151. mysql_close($db_handle);
  152. $data = array();
  153. while($row = mysql_fetch_assoc($result))
  154. {
  155. $data[] = $row;
  156. }
  157. $colNames = array_keys(reset($data));
  158. return $data;
  159. }
  160. else {
  161. print "Database NOT Found ";
  162. mysql_close($db_handle);
  163. }
  164. }
  165. public function publicGeneralSelectWithPredicatorSeperated($query){
  166. if ($this->DBH) {
  167. //echo "SELECT * from $table_name WHERE $clause";die();
  168. $STH = $this->DBH->query($query);
  169. $STH->setFetchMode(PDO::FETCH_ASSOC);
  170. // $SQL = "SELECT * FROM $table_name WHERE $clause";
  171. // //echo $SQL;die();
  172. // $result = mysql_query($SQL);
  173. // mysql_close($db_handle);
  174. return $STH;
  175. }
  176. else {
  177. print "Database NOT Found ";
  178. }
  179. }
  180. /**
  181. * A general method to insert data to a table with data
  182. */
  183. public function publicGeneralInsertStatement($query){
  184. if ($this->DBH) {
  185. $fields = "";
  186. $values = "";
  187. $SQL = $query;
  188. //echo "$SQL";die();
  189. $qurey = $this->DBH->prepare($SQL);
  190. $qurey->execute();
  191. return $this->DBH->lastInsertId();
  192. }
  193. else {
  194. print "Database NOT Found ";
  195. }
  196. }
  197. public function generalInsertStatement($table_name,$field,$data_array){
  198. if ($this->DBH) {
  199. $fields = "";
  200. $values = "";
  201. $SQL = "INSERT INTO $table_name (";
  202. foreach ($field as &$value) {
  203. $fields .= " $value ,";
  204. }
  205. $fields = substr($fields,0,-1);
  206. foreach ($data_array as &$valuee) {
  207. if ($valuee != NULL) {
  208. $values .= " '$valuee' ,";
  209. }
  210. else{
  211. $values .= " NULL ,";
  212. }
  213. }
  214. $values = substr($values,0,-1);
  215. unset($value);
  216. unset($valuee);
  217. $SQL .= $fields.") VALUES (".$values.")";
  218. //echo "$SQL";die();
  219. $qurey = $this->DBH->prepare($SQL);
  220. $qurey->execute();
  221. return $this->DBH->lastInsertId();
  222. }
  223. else {
  224. print "Database NOT Found ";
  225. }
  226. }
  227. public function generalInsert2($table_name,$fields,$data_array)
  228. {
  229. mysql_connect($this->server, $this->user_name, $this->password) or die(mysql_error());
  230. mysql_select_db($this->database) or die(mysql_error());
  231. $fields = "";
  232. $values = "";
  233. $SQL = "INSERT INTO $table_name (";
  234. foreach ($field as &$value) {
  235. $fields .= " $value ,";
  236. }
  237. $fields = substr($fields,0,-1);
  238. foreach ($data_array as &$valuee) {
  239. $values .= " '$valuee' ,";
  240. }
  241. $values = substr($values,0,-1);
  242. unset($value);
  243. unset($valuee);
  244. echo "$SQL";die();
  245. $SQL .= $fields.") VALUES (".$values.")";
  246. mysql_query($SQL);
  247. }
  248. /**
  249. A general method to update data to a table with data
  250. */
  251. public function generalUpdateStatement($SQL){
  252. // echo $SQL;
  253. // die();
  254. $db_handle = mysql_connect($this->server,$this->user_name,$this->password);
  255. mysql_query ("set character_set_client='utf8'");
  256. mysql_query ("set character_set_results='utf8'");
  257. mysql_query ("set collation_connection='utf8_general_ci'");
  258. $db_found = mysql_select_db($this->database, $db_handle);
  259. if ($db_found) {
  260. $result = mysql_query($SQL);
  261. mysql_close($db_handle);
  262. return $result;
  263. }
  264. else {
  265. print "Database NOT Found ";
  266. mysql_close($db_handle);
  267. }
  268. }
  269. /**
  270. A general method to delete a row from table
  271. */
  272. public function generalDeleteStatement($table_name,$clause){
  273. $db_handle = mysql_connect($this->server,$this->user_name,$this->password);
  274. $db_found = mysql_select_db($this->database, $db_handle);
  275. if ($db_found) {
  276. //echo "DELETE FROM $table_name WHERE $clause";die();
  277. $result = mysql_query("DELETE FROM $table_name WHERE $clause");
  278. mysql_close($db_handle);
  279. return $result;
  280. }
  281. else {
  282. print "Database NOT Found ";
  283. mysql_close($db_handle);
  284. }
  285. }
  286. }
  287. ?>