Advertisement
Guest User

Untitled

a guest
Apr 16th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. <?php
  2. class QuoteDatabase{
  3. var $dbAddress;
  4. var $dbUsername;
  5. var $dbPassword;
  6. var $dbDatabase;
  7. var $dbPort;
  8. var $sqlDb;
  9. var $debug;
  10.  
  11. function QuoteDatabase($address, $user, $pw, $db, $port){
  12. $this->dbAddress = $address;
  13. $this->dbUsername = $user;
  14. $this->dbPassword = $pw;
  15. $this->dbDatabase = $db;
  16. $this->dbPort = $port;
  17. $this->debug = true;
  18. }
  19.  
  20. private function open(){
  21. $this->sqlDb = new mysqli(
  22. $this->dbAddress,
  23. $this->dbUsername,
  24. $this->dbPassword,
  25. $this->dbDatabase,
  26. $this->dbPort
  27. );
  28.  
  29. // On error print Error Numer and Message
  30. if ($this->sqlDb->connect_errno) {
  31. echo 'Failed to connect to MySQL: (' . $this->sqlDb->connect_errno . ') ' . $this->sqlDb->connect_error;
  32. }
  33. }
  34.  
  35. function getQuoteById($id) {
  36. // TODO: Validate input because SQLI
  37. $sqlQuery = $this->buildQuoteByIdQuery($id);
  38.  
  39. $this->open(); // TODO: have to close?
  40. $result = $this->sqlDb->query($sqlQuery);
  41.  
  42. if ($result === false) {
  43. echo "The Database returned an Error!\n";
  44. if(debug) die(mysql_error());
  45.  
  46. die('An error occured, please try again later');
  47. }
  48. $quote = $this->parseResultsToQuote($result);
  49.  
  50. return $quote;
  51. }
  52.  
  53. function getRandomQuote(){
  54. $min = 1;
  55. $max = $this->getHighestId();
  56. $id = rand($min, $max);
  57. return $this->getQuoteById($id);
  58. }
  59.  
  60. function customQuery($query) {
  61. $this->open();
  62. $result = $this->sqlDb->query($query);
  63.  
  64. if ($result === false) {
  65. echo "The Database returned an Error!\n";
  66. if(debug) die(mysql_error());
  67.  
  68. die('An error occured, please try again later');
  69. }
  70.  
  71. return $result;
  72. }
  73.  
  74. private function parseResultsToQuote($results) {
  75. while ($row = $results->fetch_array(MYSQLI_ASSOC)) $rows[] = $row;
  76. if(!isset($rows))
  77. return NULL;
  78.  
  79. foreach ($rows as $row) {
  80. $quoteId = $row['q_id'];
  81. $quoteYear = $row['year'];
  82. $quoteSubmitter = $row['submitter'];
  83. $quoteGame = $row['game'];
  84. $gameStyle = $row['style'];
  85.  
  86. $lineName = $row['name'];
  87. $lineText = $row['line'];
  88. $lineTeam = $row['team'];
  89. $lineIndex = $row['l_index'];
  90.  
  91. $line = new QuoteLine ($lineName, $lineText, $lineTeam, $lineIndex);
  92. $lines[] = $line;
  93. }
  94.  
  95. $q = new Quote($quoteId, $lines, $quoteYear, $quoteSubmitter, $quoteGame, $gameStyle);
  96. return $q;
  97. }
  98.  
  99. function getHighestId() {
  100. $sqlStr = 'SELECT MAX(id) as id FROM quotes';
  101. $result = $this->customQuery($sqlStr);
  102. while ($row = $result->fetch_array(MYSQLI_ASSOC)) $rows[] = $row;
  103. foreach($rows as $row) $id = $row['id'];
  104. return $id;
  105. }
  106.  
  107. private function buildQuoteByIdQuery($id) {
  108. $sqlQuery = 'SELECT' .
  109. ' quotes.id AS q_id ,' .
  110. ' quotes.year AS year ,' .
  111. ' quotes.submitted_by AS submitter ,' .
  112. ' quote_lines.name AS name ,' .
  113. ' quote_lines.line AS line ,' .
  114. ' quote_lines.line_index AS l_index ,' .
  115. ' teams.name AS team ,' .
  116. ' games.name AS game ,' .
  117. ' games.cssfile AS style ' .
  118.  
  119. ' FROM' .
  120. ' quotes, quote_lines, games, teams ' .
  121.  
  122. ' WHERE quotes.id = ' . $id .
  123. ' AND quote_lines.quote_id = quotes.id ' .
  124. ' AND quotes.game_id = games.id ' .
  125. ' AND quote_lines.team_id = teams.id ' .
  126.  
  127. ' ORDER BY quote_lines.line_index ASC ' .
  128. ';';
  129.  
  130. return $sqlQuery;
  131. }
  132. }
  133. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement