<?php
/* Games.php */ 
	$hostname = 'localhost';
	$username = 'narKampen';
	$pass = 'narKampenPassword';
	$database = 'narKampen';
	
	$conn = new mysqli($hostname, $username, $pass, $database);
	if (!$conn) {
		die("Connection Failed. ". mysqli_connect_error());
	}
	mysqli_set_charset($conn,'utf8');
	
	$userId = $conn->real_escape_string($_POST['userId']);
		
	$sql = "SELECT * FROM game WHERE id IN (SELECT gameId FROM gamePlayers WHERE playerId = $userId)";
	$result = $conn->query($sql);
	
	
	if ($result->num_rows > 0) {
		$returnArray = array();
		$i = 0;
		while ($data = $result->fetch_assoc()) {
			foreach ($data as $key => $value) {
				$returnArray[$i][$key] = $value;
			}
			$i++;
		}
		echo json_encode($returnArray);
		
	} else {
		echo "No games found for user";
	}
	$conn->close();
	
?>