Fixed AJAX History DB query