Question.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. error_reporting( E_ALL );
  3. $hostname = 'localhost';
  4. $username = 'narKampen';
  5. $pass = 'IfRLzj2HJBXA9eei';
  6. $database = 'narKampen';
  7. $conn = new mysqli($hostname, $username, $pass, $database);
  8. $gameId = $_POST["gameId"];
  9. if (!$conn) {
  10. die("Connection Failed. ". mysqli_connect_error());
  11. }
  12. mysqli_set_charset($conn,'utf8');
  13. $sql = "SELECT questions.id, question, answer, categoryId as category, name, r,g,b,a FROM questions INNER JOIN questionToCategory ON questions.id = questionToCategory.questionId INNER JOIN category on category.id = questionToCategory.categoryId WHERE questionToCategory.categoryId IN (SELECT categoryId FROM gameCategories WHERE gameId = $gameId) ORDER BY RAND() limit 1";
  14. $result = mysqli_query($conn, $sql);
  15. $json = array();
  16. if (mysqli_num_rows($result) > 0) {
  17. while ($row = mysqli_fetch_assoc($result)) {
  18. $json = array('id' => $row['id'],
  19. 'question' => $row['question'],
  20. 'answer' => $row['answer'],
  21. 'category' => $row['category'],
  22. 'categoryName' => $row['name'],
  23. 'r' => $row['r'],
  24. 'g' => $row['g'],
  25. 'b' => $row['b'],
  26. 'a' => $row['a'],);
  27. }
  28. $jsonString = json_encode($json);
  29. echo $jsonString;
  30. }
  31. ?>