Question.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. $answeredQuestions = $_POST["answeredQuestions"];
  10. if (!$conn) {
  11. die("Connection Failed. ". mysqli_connect_error());
  12. }
  13. mysqli_set_charset($conn,'utf8');
  14. $sql = "SELECT questions.id, question, answer, categoryId as category, name, r,g,b,a FROM questions
  15. INNER JOIN questionToCategory ON questions.id = questionToCategory.questionId
  16. INNER JOIN category on category.id = questionToCategory.categoryId
  17. WHERE questionToCategory.categoryId IN (SELECT categoryId FROM gameCategories WHERE gameId = $gameId) AND
  18. questions.id NOT IN ($answeredQuestions)
  19. ORDER BY RAND() limit 1";
  20. $result = mysqli_query($conn, $sql);
  21. $json = array();
  22. if (mysqli_num_rows($result) > 0) {
  23. while ($row = mysqli_fetch_assoc($result)) {
  24. $json = array('id' => $row['id'],
  25. 'question' => $row['question'],
  26. 'answer' => $row['answer'],
  27. 'category' => $row['category'],
  28. 'categoryName' => $row['name'],
  29. 'r' => $row['r'],
  30. 'g' => $row['g'],
  31. 'b' => $row['b'],
  32. 'a' => $row['a'],);
  33. }
  34. $jsonString = json_encode($json);
  35. echo $jsonString;
  36. }
  37. ?>