Reputation: 76
All, I have a php file which runs a MYSQL query and places the result in an array. The table has several fields, one has the characters '-' and '%' as part of a varchar string. When I pass this back in a JSON array it simply dies. Here are the code sections..
PHP file
// fetch merit guidelines
$meritGuideline = array();
$sql = "SELECT smg.id, year, ea.rating, ratioUpper, ratioLower, guideline, guidelineNum FROM spot_merit_guide smg JOIN epm_annual ea ON ea.id = smg.rating WHERE year = '$spotYear'";
$query = mysqli_query($con, $sql);
while ($row = mysqli_fetch_assoc($query)) {
$meritGuideline[] = $row;
}
mysqli_free_result($query);
Then I return it with this
echo json_encode(array('meritGuideline' => $meritGuideline));
The mysql table field guideline contains the string 0 - 4%.
Upvotes: 0
Views: 1066
Reputation: 850
First, try encoding the data to UTF-8:
// fetch merit guidelines
$meritGuideline = array();
$sql = "SELECT smg.id, year, ea.rating, ratioUpper, ratioLower, guideline, guidelineNum FROM spot_merit_guide smg JOIN epm_annual ea ON ea.id = smg.rating WHERE year = '$spotYear'";
$query = mysqli_query($con, $sql);
while ($row = mysqli_fetch_assoc($query)) {
$meritGuideline[] = utf8_encode($row); // ENCODE TO UTF-8
}
mysqli_free_result($query);
Then run this for debug purposes:
echo json_encode(array('meritGuideline' => $meritGuideline));
echo 'Error: ' . json_last_error_msg();
If the first line above does output your JSON, then the problem was most likely a hidden UTF-8 only special/invisible space character, which are often mischievous when dealing with JSON encoding or even Javascript code parsing in the browser. Here are some examples of these "special" space characters:
No-break space: http://www.fileformat.info/info/unicode/char/00a0/index.htm
Zero-width space: http://www.fileformat.info/info/unicode/char/200b/index.htm
If the first line still doesn't output anything, then the second line will allow you to see the exact error that's happening inside PHP's JSON encoder.
Upvotes: 1