10);"; mysqli_query($res, $query); $query = "SELECT id FROM gps WHERE id = '$id' "; $results = mysqli_query($res, $query); //Execute the SQL command if (empty($results->fetch_row()[0])){ $query = "INSERT INTO gps(id,latitude,longitude,idMusic,dateLog) VALUES('$id','$latitude','$longitude','$idMusic',NOW());"; //Insert into the database the new data and new information about this user }else{ $query = "UPDATE gps SET latitude='$latitude', longitude='$longitude', idMusic='$idMusic', dateLog=NOW() WHERE id='$id'"; //Delete the actual line and replace this line with the next lines } mysqli_query($res, $query); $query = "SELECT * FROM gps WHERE id != '$id'"; //Browse all the database $results = mysqli_query($res, $query); //Execute the SQL command $listUser = []; //Set the listUser to an empty list while ($row = $results->fetch_row()) { //For all the row in the database $lat2 = $row[1]; //Set $lat2 to the latitude of the user who is in the actual row $lng2 = $row[2]; //Set $lng2 to the latitude of the user who is in the actual row $userID = $row[0]; //Set $userID to the username of the user who is in the actual row $idMusic = $row[3]; //Set $idMusic to the id of the actual song of the user who is in the actual row $dist = metersBetweenTwoUser($latitude, $longitude, $lat2, $lng2); //With the function meters, determinate the distance between the current user and the user who is in the actual row if ($dist <= 100) { //If the user in the actual row is less than 100 meters away of the current user $listUser[] = ['user' => $userID, 'music' => $idMusic]; //Add the username and the ID of the song that user who is in the actual row is listening } } return $listUser; //Return an array } else { //If the method POST return nothing return 3; //Return a code error } } print(json_encode(insertUserAndReturnList()));