<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header("Access-Control-Allow-Headers: *");
$name = $_GET['name'];
$servername = "localhost";
$username = "hossan";
$password = 'k/oVEq4vLATIoY';
$dbname = "zhossan";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
//echo "Connected successfully";



if (isset($name)) {
    $sql = "SELECT * FROM Sessie WHERE Naam=\"$name\" ORDER BY Score DESC LIMIT 10";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
        // output data of each row and put them into a json array
        while ($row = $result->fetch_assoc()) {
            //echo "Name: " . $row["Naam"]. " -Score:" . $row["Score"]. "-Datum:" . $row["Datum"]. "<br>";
            $data[] = $row;
            $json = json_encode($data);
        }
    } else {
        echo "0 results for requested name";
    }

}
else {
    $sql = "SELECT * FROM Sessie ORDER BY Score DESC LIMIT 10";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
        // output data of each row and put them into a json array
        while ($row = $result->fetch_assoc()) {
            //echo "Name: " . $row["Naam"]. " -Score:" . $row["Score"]. "-Datum:" . $row["Datum"]. "<br>";
            $data[] = $row;
            $json = json_encode($data);
        }
    } else {
        echo "0 results";
    }
    
}


echo $json;
$conn->close();
?>