Return Specific Data from jQuery/AJAX and PHP File

Since my last post was apparently "too broad" for Stack Overflow, I'll post multiple questions with very specific questions. For this post, I would like some assistance with returning specific data from a MySQL database using an AJAX call to a PHP file. I'm able to get the data to return, but it does so in a loop and the response does not match. For example, if you search for "New York", it will return, "You searched for a city near Los Angeles" or something similar, because it's returning the last row of the data set. I'd like to know how to correct this and return only the New York value.

jQuery("#submit").on("click", function(e) {
    e.preventDefault();
    console.log('submitted');

    var getDIFF = jQuery("#getDIFF").val();
    var getCity = jQuery("#getCity").val();

    jQuery.ajax({
      type: "GET",
        url: "/api.php?getDIFF="+getDIFF+"&getCity="+getCity,
      data: {
        getDIFF: getDIFF,
        getCity: getCity
      },
      //dataType: "json",

    }) 

      .fail(function(jqXHR, textstatus) { 
            alert("request failed: " + textstatus); console.log(jqXHR); 
    })

        .done(function(data) {
            var result = jQuery.parseJSON(data);

            jQuery.each(result, function(key, value) {
          //console.log(value.DIFF_ID + " " + value.LATITUDE);
          var lat = value.LATITUTDE;
          var long = value.LONGITUDE;
          jQuery("#airOut").html("You searched for results at or near " + value.NAME); // This is what returns the last row of the data set instead of what I searched for.
      });
    })     
});

I'm aware that I put it into a loop, but I don't know why it's returning the last row of that data set. That's because there could be multiple cities with the same name or something similar. I don't know if it's needed, but here is the PHP file:

$result_array = array();

$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);

if(!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

if(isset($_GET['getDIFF']) OR isset($_GET['getCity'])) {
    $getDIFF = $_GET['getDIFF'];
    $getCity = $_GET['getCity'];

$getData = "SELECT * FROM table1 WHERE DIFF = '$getDIFF' OR SERVCITY = '$getCity' LIMIT 10";
$result = mysqli_query($conn, $getData);

    //echo $result;
    //echo mysqli_num_rows($result);

    if(mysqli_num_rows($result) > 0) {
        while($row = mysqli_fetch_assoc($result)) {
            array_push($result_array, $row);
        } 

    } else {

        echo "No results found.";
    }
}

//header('Content-type: application/json');
echo json_encode($result_array);

Thank you for your help.


I am not a pro or expert but I have used ajax request like a millions of times. So from that perspective I recommend you to use the latest version of jquery and replace your ajax structure like this


$("#submit").on("click", function(e){
    e.preventDefault();
//     console.log('submitted');

    var getDIFF = jQuery("#getDIFF").val();
    var getCity = jQuery("#getCity").val();

    $.ajax({
        type: "GET",
        url: "/api.php?getDIFF="+getDIFF+"&getCity="+getCity,
        //data: {
        //  getDIFF: getDIFF,
        //  getCity: getCity // no need data because you requesting "get" with query string (?getDIFF="+getDIFF+"&getCity="+getCity) 
        //},
        success: function(data){
            var result = jQuery.parseJSON(data);

            $.each(result, function(key, value) {
                //console.log(value.DIFF_ID + " " + value.LATITUDE);
                var lat = value.LATITUTDE;
                var long = value.LONGITUDE;
                $("#airOut").text("You searched for results at or near " + value.NAME); // This is what returns the last row of the data set instead of what I searched for.
            });
        },
        error: function(){
            alert("request failed"); 
            console.log(error);
        }

    });

});

For php I think it's look okay. If you not sure what is your query is returning you can run your query directly in mysql and see if you have the correct set of data.