I'm getting the error 'Unexpected end of JSON input' when using JSON to retrieve data from a MySQL db
The following code snippet calls GetProfileTest.php which executes a select statement and returns a record. The record contains the field value 'Athlète'. If delete è from the field the codes runs without an error. The db is uses utf8_unicode_ci collation.
The weird thing is the code works fine on another website with identical tables.
Can anybody provide an insight has to what causes the error. Eliminating the French characters is not an option.
$.ajax({
method: 'GET',
url: '/wp-content/PHP/GetProfileTest.php',
data :{"CFF":CFF,"LANG":LANG},
"timeout": 5000
})
.fail(function (jqXHR, textStatus, errorThrown) {
// Request failed. Show error message to user.
alert('Data get failed: '+errorThrown);
})
.done(function(data, textStatus, jqXHR ) {
var result= data;
if ($.type(result)=="string") {
alert("No profile found " + CFF);
};
Here is GetProfileTest.php
<?php
// Revieve ID and query db to get profile
// get connection data)
require('Connection.php');
/* Create connection */
$conn = mysqli_connect($host,$username, $password, $dbname);
// Check connection
if (!$conn){
echo 'Connection error: '. mysqli_connect_error();
}
// SQL query to get profile for the ID provided
$CFF = $_GET['CFF'];
$LANG = $_GET['LANG'];
if ($LANG == 'EN') {
$sql = "SELECT CFF_Num,VeteranName,Role_En FROM VeteranProfileView WHERE CFF_Num='".$CFF."'";
} else {
$sql = "SELECT CFF_Num,VeteranName,Role_Fr FROM VeteranProfileView WHERE CFF_Num='".$CFF."'";
}
$sql = "SELECT CFF_Num,VeteranName,Role_Fr FROM VeteranProfileView WHERE CFF_Num='".$CFF."'";
// Run Query and get result
if (!$result = mysqli_query($conn,$sql)) {
echo 'Query failed: '. mysqli_error($conn);
}
/* If there are results from query push to result array */
$result_array = array();
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
array_push($result_array, $row);
}
/* send a JSON encoded array to client */
header('Content-type: application/json');
echo json_encode($result_array);
} else {
echo ('No profile found');
}
mysqli_free_result($result);
mysqli_close($conn);