Database.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. include_once 'Team.php';
  3. include_once 'SoccerMatch.php';
  4. include_once 'League.php';
  5. class Database
  6. {
  7. private $conn;
  8. function openConnection()
  9. {
  10. $dbhost = 'nordh.xyz';
  11. $dbuser = 'OddsNy';
  12. $dbPass = 'Odds1_Ny_Password';
  13. $db = 'new_odds';
  14. try {
  15. $this->conn = new PDO(
  16. "mysql:host=$dbhost;dbname=$db",
  17. $dbuser,
  18. $dbPass
  19. );
  20. $this->conn->setAttribute(
  21. PDO::ATTR_ERRMODE,
  22. PDO::ERRMODE_EXCEPTION
  23. );
  24. } catch (Exception $e) {
  25. echo 'Exception ' . $e->getMessage();
  26. }
  27. }
  28. function closeConnection($conn)
  29. {
  30. $conn = null;
  31. }
  32. function getConnection()
  33. {
  34. if (!isset($this->conn)) {
  35. $this->openConnection();
  36. }
  37. return $this->conn;
  38. }
  39. function getTodaysPrioMatches()
  40. {
  41. $this->openConnection();
  42. $result = [];
  43. $sql = 'SELECT sr.*,
  44. ht.id as homeTeamId, ht.name as homeTeamName, ht.countryId as homeTeamCountryId, ht.leagueId as homeTeamLeagueId,
  45. awt.id as awayTeamId, awt.name as awayTeamName, awt.countryId as awayTeamCountryId, awt.leagueId as awayTeamLeagueId,
  46. l.name as leagueName,
  47. c.name as countryName
  48. FROM SoccerResults sr
  49. INNER JOIN Team ht on ht.id = sr.homeTeamId
  50. INNER JOIN Team awt on awt.id = sr.awayTeamId
  51. INNER JOIN League l ON l.id = sr.leagueId
  52. INNER JOIN Country c ON c.id = sr.countryId
  53. WHERE sr.leagueId IN (SELECT id FROM League WHERE prio = 1) AND DATE(gameDate) = DATE(NOW())';
  54. $stat = $this->conn->query($sql);
  55. $matches = $stat->fetchAll();
  56. foreach ($matches as $match) {
  57. $homeTeam = new Team(
  58. $match['homeTeamId'],
  59. $match['homeTeamName'],
  60. $match['homeTeamCountryId'],
  61. $match['countryName'],
  62. $match['homeTeamLeagueId'],
  63. $match['leagueName']
  64. );
  65. $awayTeam = new Team(
  66. $match['awayTeamId'],
  67. $match['awayTeamName'],
  68. $match['awayTeamCountryId'],
  69. $match['countryName'],
  70. $match['awayTeamLeagueId'],
  71. $match['leagueName']
  72. );
  73. $m = new SoccerMatch(
  74. $match['id'],
  75. $homeTeam,
  76. $awayTeam,
  77. $match['odds1'],
  78. $match['oddsX'],
  79. $match['odds2'],
  80. $match['homeScore'],
  81. $match['awayScore'],
  82. $match['gameDate'],
  83. $match['season']
  84. );
  85. $result[] = $m;
  86. }
  87. $this->closeConnection($this->conn);
  88. return $result;
  89. }
  90. public function getLeagueTable($leagueId, $season, $countryId, $date)
  91. {
  92. $this->openConnection();
  93. $result = [];
  94. $sql = "SELECT teamName.name AS teamName,
  95. COUNT(*) AS played,
  96. SUM(CASE WHEN homeScore > awayScore THEN 1 ELSE 0 END) AS wins,
  97. SUM(CASE WHEN awayScore > homeScore THEN 1 ELSE 0 END) AS lost,
  98. SUM(CASE WHEN homeScore = awayScore THEN 1 ELSE 0 END) AS draws,
  99. SUM(homeScore) AS homeScore,
  100. SUM(awayScore) AS awayScore,
  101. SUM(homeScore - awayScore) AS goal_diff,
  102. SUM(CASE WHEN homeScore > awayScore THEN 3 ELSE 0 END + CASE WHEN homeScore = awayScore THEN 1 ELSE 0 END) AS score,
  103. season
  104. FROM
  105. (SELECT hometeamId AS team,
  106. homeScore,
  107. awayScore,
  108. season,
  109. gameDate,
  110. leagueId AS league,
  111. countryId AS country
  112. FROM SoccerResults
  113. WHERE homeScore != -1 AND awayScore != -1 AND DATE(gameDate) < DATE(?)
  114. UNION ALL
  115. SELECT awayteamId AS team,
  116. awayScore,
  117. homeScore,
  118. season,
  119. gameDate,
  120. leagueId AS league,
  121. countryId AS country
  122. FROM SoccerResults
  123. WHERE homeScore != -1 AND awayScore != -1 AND DATE(gameDate) < DATE(?)) a
  124. INNER JOIN Team AS teamName ON a.team = teamName.id
  125. WHERE season = ? AND league = ? AND country = ?
  126. GROUP BY teamName.id
  127. ORDER BY score DESC, goal_diff DESC;";
  128. $stmt = $this->conn->prepare($sql);
  129. $stmt->bindParam(1, $season);
  130. $stmt->bindParam(2, $leagueId);
  131. $stmt->bindParam(3, $countryId);
  132. $stmt->bindParam(4, $date);
  133. $stmt->execute();
  134. while ($row = $stmt->fetch()) {
  135. $ts = [
  136. 'teamName' => $row['teamName'],
  137. 'wins' => $row['wins'],
  138. 'lost' => $row['lost'],
  139. 'draws' => $row['draws'],
  140. 'score' => $row['score'],
  141. 'homeScore' => $row['homeScore'],
  142. 'awayScore' => $row['awayScore'],
  143. 'goal_diff' => $row['goal_diff'],
  144. ];
  145. array_push($result, $ts);
  146. }
  147. $this->closeConnection($this->conn);
  148. return $result;
  149. }
  150. function getLeagueInfo($leagueId)
  151. {
  152. $this->openConnection();
  153. $sql = 'SELECT * FROM League WHERE id = ?';
  154. $result = null;
  155. try {
  156. $stat = $this->conn->prepare($sql);
  157. $stat->bindParam(1, $leagueId);
  158. $stat->execute();
  159. while ($row = $stat->fetch()) {
  160. $result = new League(
  161. $row['id'],
  162. $row['name'],
  163. $row['scoringDiffLastGame'],
  164. $row['scoringTotal'],
  165. $row['winLossRatioHomeAndAway'],
  166. $row['winLossRatio'],
  167. $row['drawDiffHomeAway'],
  168. $row['drawDiffTotalGoals'],
  169. $row['drawWinningForm'],
  170. $row['drawWinningFormHomeAway']
  171. );
  172. }
  173. } catch (PDOException $e) {
  174. echo 'Connection failed: ' . $e->getMessage();
  175. }
  176. $this->closeConnection($this->conn);
  177. return $result;
  178. }
  179. }