PlayerSearch.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. if (!$conn) {
  9. die("Connection Failed. ". mysqli_connect_error());
  10. }
  11. mysqli_set_charset($conn,'utf8');
  12. $function = $_POST["f"];
  13. if ($function === "PlayerSearch") {
  14. $searchString = $_POST["SearchString"];
  15. $sql = "SELECT id, username FROM users WHERE username LIKE '%$searchString%' LIMIT 20";
  16. $result = mysqli_query($conn, $sql);
  17. $json = array();
  18. if (mysqli_num_rows($result) > 0) {
  19. while ($row = mysqli_fetch_assoc($result)) {
  20. $json[] = array('id' => $row['id'],
  21. 'username' => $row['username']);
  22. }
  23. $jsonString = json_encode($json);
  24. echo $jsonString;
  25. } else {
  26. echo "";
  27. }
  28. } else if ($function === "FindRandomPlayers") {
  29. $currentPlayerId = $_POST['playerId'];
  30. $sql = "SELECT id, username FROM users WHERE id != $currentPlayerId ORDER BY RAND() LIMIT 10";
  31. $result = mysqli_query($conn, $sql);
  32. if (mysqli_num_rows($result) > 0) {
  33. $json = array();
  34. while ($row = mysqli_fetch_assoc($result)) {
  35. $json[] = array('id' => $row["id"],
  36. 'username' => $row["username"]);
  37. }
  38. echo json_encode($json);
  39. } else {
  40. echo "";
  41. }
  42. } else {
  43. echo "No Function Found";
  44. }
  45. ?>