Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.74 KB | None | 0 0
  1. function callPHP() {
  2. $.ajax ({
  3. type: "GET",
  4. datatype: "application/json",
  5. url: "MySQLDao.php",
  6. data: { action : 'getResults' },
  7. success: function(output) {
  8.  
  9. alert(output);
  10.  
  11. }
  12.  
  13. });
  14.  
  15. }
  16.  
  17. callPHP();
  18.  
  19. <?php
  20.  
  21. require("Conn.php");
  22.  
  23. header("Access-Control-Allow-Origin: *");
  24. error_reporting(E_ALL); ini_set('display_errors', 1);
  25.  
  26. //Class for holding queries
  27. class MySQLDao
  28. {
  29. private $dbhost = null;
  30. private $dbuser = null;
  31. private $dbpass = null;
  32. private $mysqli = null;
  33. private $dbname = null;
  34. private $result = null;
  35.  
  36.  
  37. //constructor
  38. function __construct()
  39. {
  40. $this->dbhost = Conn::$dbhost;
  41. $this->dbuser = Conn::$dbuser;
  42. $this->dbpass = Conn::$dbpass;
  43. $this->dbname = Conn::$dbname;
  44. }
  45.  
  46. //Attempt a connection to the database
  47. public function openConnection()
  48. {
  49. //Try and connect to the database
  50. $this->mysqli = new mysqli($this->dbhost, $this->dbuser, $this->dbpass, $this->dbname);
  51. //If the connection threw an error, report it
  52. if (mysqli_connect_errno())
  53. {
  54. return false;
  55. }
  56. else
  57. {
  58. return true;
  59. }
  60. }
  61.  
  62. //Get method for retrieving the database conection
  63. public function getConnection()
  64. {
  65. return $this->mysqli;
  66. }
  67.  
  68. //Close the connection to the database
  69. public function closeConnection()
  70. {
  71. //If there is a connection to the database then close it
  72. if ($this->mysqli != null)
  73. $this->mysqli->close();
  74. }
  75.  
  76. //-----------------------------------QUERY METHODS-------------------------------------
  77.  
  78. public function generateRoomID()
  79. {
  80. $sql = "INSERT INTO room (room_id) VALUES (null);";
  81. $result = $this->mysqli->query($sql);
  82. if ($result == true)
  83. {
  84. $toReturn["status"] = true;
  85. $toReturn["roomID"] = $this->mysqli->insert_id;
  86. return $toReturn;
  87. }
  88. else
  89. {
  90. $toReturn["status"] = false;
  91. $toReturn["message"] = mysql_error($this->mysqli);
  92. return $toReturn;
  93. }
  94. }
  95.  
  96. public function saveRoom($data)
  97. {
  98. $roomID = $data[0];
  99. $roomDescription = $data[1];
  100. $columns = $data[2];
  101. $rows = $data[3];
  102.  
  103. $this->mysqli->autocommit(FALSE);
  104.  
  105. $this->mysqli->query("UPDATE room SET room_description='".$roomDescription."' WHERE room_id='".$roomID."';");
  106.  
  107. for ($i = 0; $i<count($columns); $i++)
  108. {
  109. for ($j = 1; $j<=$rows[$i]; $j++)
  110. {
  111. $currentLabel = "{$columns[$i]}{$j}";
  112. $this->mysqli->query("INSERT INTO shelf (shelf_label) VALUES ('".$currentLabel."');");
  113. $shelfID = $this->mysqli->insert_id;
  114. $this->mysqli->query("INSERT INTO room_shelf (room_id, shelf_id) VALUES ('".$roomID."','".$shelfID."');");
  115. }
  116. }
  117.  
  118. if ($this->mysqli->commit())
  119. {
  120. $toReturn["status"] = true;
  121. $toReturn["message"] = "Room Created";
  122. return $toReturn;
  123. }
  124. else
  125. {
  126. $this->mysqli->rollback();
  127. $toReturn["status"] = false;
  128. $toReturn["message"] = "SQL Error";
  129. return $toReturn;
  130. }
  131. }
  132.  
  133. public function updateShelf($data)
  134. {
  135. $shelfID = $data[0];
  136. $itemName = $data[1];
  137. }
  138.  
  139. public function getRoomDetails($data)
  140. {
  141. $roomID = $data[0];
  142.  
  143. $sql = "SELECT room.room_description, shelf.shelf_label, shelf.shelf_id FROM room INNER JOIN room_shelf ON room.room_id=room_shelf.room_id INNER JOIN shelf ON shelf.shelf_id=room_shelf.shelf_id WHERE room.room_id='".$roomID."';";
  144.  
  145. $result = $this->mysqli->query($sql);
  146.  
  147. if (mysqli_num_rows($result) > 0)
  148. {
  149. $toReturn["status"] = true;
  150. $toReturn["message"] = "Room Found";
  151. $toReturn["room_description"] = $row['room_description'];
  152. $shelves = [];
  153.  
  154. foreach ($result as $row)
  155. {
  156. $currentShelf["shelf_label"] = $row['shelf_label'];
  157. $currentShelf["shelf_id"] = $row['shelf_id'];
  158. array_push($shelves, $currentShelf);
  159. }
  160. $toReturn["shelves"] = $shelves;
  161. return $toReturn;
  162. }
  163. else
  164. {
  165. $toReturn["status"] = false;
  166. $toReturn["title"] = "Error";
  167. $toReturn["message"] = "Room Not Found";
  168. return $toReturn;
  169. }
  170.  
  171. }
  172.  
  173. public function getResults($data)
  174. {
  175.  
  176. $sql = "SELECT room.room_description FROM room WHERE room.room_id = 1";
  177.  
  178. $result = $this->mysqli->query($sql);
  179.  
  180. echo json_encode($result);
  181.  
  182. }
  183.  
  184. }
  185.  
  186. $daoObj = new MySQLDao();
  187. $daoObj->getResults();
  188.  
  189. ?>
  190.  
  191. <?php
  192. class Conn
  193. {
  194. public static $dbhost = "xxx";
  195. public static $dbname = "xxx";
  196. public static $dbuser = "xxx";
  197. public static $dbpass = "xxx";
  198. }
  199. ?>
  200.  
  201. <?php
  202. require("Conn.php");
  203. require("MySQLDao.php");
  204.  
  205. $handle = fopen("php://input", "rb");
  206. $raw_post_data = '';
  207. while (!feof($handle)) {
  208. $raw_post_data .= fread($handle, 8192);
  209. }
  210. fclose($handle);
  211.  
  212. if (empty($raw_post_data))
  213. {
  214. $returnValue["status"] = false;
  215. $returnValue["title"] = "Error";
  216. $returnValue["message"] = "No Data Recieved";
  217. echo json_encode($returnValue);
  218. return;
  219. }
  220. else
  221. {
  222. $dao = new MySQLDao();
  223. if ($dao->openConnection() == false)
  224. {
  225. $returnValue["status"] = false;
  226. $returnValue["title"] = "Error";
  227. $returnValue["message"] = "Connection Could Not Be Established Between Server And Database";
  228. echo json_encode($returnValue);
  229. }
  230. else
  231. {
  232. //Decodes data, dont change
  233. $body = json_decode($raw_post_data, true);
  234. $recieved = $body["data"];
  235.  
  236. //Gets the result of a query
  237. //$result = $dao->MySQLDaoMethodName(parameters);
  238.  
  239. //Return the result of the query
  240. echo json_encode($result);
  241. }
  242. $dao->closeConnection();
  243. return;
  244. }
  245. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement