I often have some JavaScript which receives data from a server's database and presents it on the page.
$.getJSON( 'ajax.php',{id:$('bla').val()}, function(json) {
$('#myClone').clone().removeAttr('id').text('json.a').appendTo(whatever)
});
ajax.php
$sql='SELECT a,b,c FROM t WHERE id=?';
$stmt = $conn->prepare($sql);
$data=array($_GET['id']);
$stmt->execute($data);
header('Content-Type: application/json;');
echo(json_encode($stmt->fetchAll(PDO::FETCH_ASSOC)));
A problem arises when the database contains NULL for a given column, and the associated JSON includes null without quotes, HTTP transmits the JSON string still with null without quotes, but JavaScript/jQuery appears to convert it into a string "null", and null is displayed on the page instead of the desired empty string.
How is this best rectified? Should be be done so server-side or client-side? If server-side, should it be done at the database level using something like SELECT IFNULL(a,""), or the PHP application level?