Advertisement
carefulnow

getAccountInfo.php

Jun 2nd, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. <?php
  2.  
  3. require_once("../init.php");
  4.  
  5. /**
  6. * This file returns account information. It's designed to be run from an
  7. * AJAX request, hence it been rather standalone.
  8. *
  9. * @param int $_GET["userID"]
  10. * @param int $_GET["mode"]
  11. *
  12. * $_GET["mode"]:
  13. * unset/unrecognised: both
  14. * 1: username only
  15. * 2: profileImg only
  16. *
  17. * Echoes out JSON.
  18. */
  19.  
  20. try {
  21.  
  22. new Database();
  23.  
  24. // throw new PDOException("some error...");
  25.  
  26. if (!isset($_GET["id"]))
  27. throw new Exception("ID is unset.");
  28.  
  29. if (!isset($_GET["mode"])) {
  30. $stmt = Database::$db->prepare("SELECT username, profileImg FROM users WHERE id = :id");
  31. } else {
  32.  
  33. switch ($_GET["mode"]) {
  34. case "1":
  35. $stmt = Database::$db->prepare("SELECT username FROM users WHERE id = :id");
  36. break;
  37. case "2":
  38. $stmt = Database::$db->prepare("SELECT profileImg FROM users WHERE id = :id");
  39. break;
  40. default:
  41. $stmt = Database::$db->prepare("SELECT username, profileImg FROM users WHERE id = :id");
  42. }
  43. }
  44.  
  45. $stmt->execute(array("id" => $_GET["id"]));
  46.  
  47. if ($stmt->rowCount() < 1)
  48. throw new Exception("A user with that ID was not found.");
  49.  
  50. $json = json_encode($stmt->fetch(PDO::FETCH_ASSOC));
  51.  
  52. if (!$json)
  53. throw new Exception("Return value could not be encoded into JSON.");
  54.  
  55. echo $json;
  56.  
  57. } catch (Exception $e) {
  58.  
  59. echo json_encode(array("errorMsg" => get_class($e) . ": " . $e->getMessage()));
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement