I'm trying to use a PHP file to pull data from a database and convert it to JSON using a $.getJSON call, but I just get the following error:
XML Parsing Error: no element found Location: moz-nullprincipal:{af08ce9b-940c-41a3-bd20-e4e5ad1a6d23} Line Number 17, Column 3:
?>
--^
Everything I can find says this is because I'm trying to pull the data from another domain, but this isn't the case for me; the website is a Maven web project that's running on a local Glassfish web server.
Here's my PHP file:
<?php
$dsn = "mysql:host=localhost;dbname=oaaa";
$username = "username";
$password = "password";
$pdo = new PDO($dsn, $username, $password);
$rows = array();
if (isset($_GET['activity'])) {
$stmt = $pdo->prepare("SELECT * FROM events WHERE type = ? ORDER BY date");
$stmt->execute(array($_GET['activity']));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
echo json_encode($rows);
?>
Here's my JavaScript file:
$(document).ready(function() {
$.getJSON('php/query-database.php', {activity: 'Abseiling'}, function(data) {
$.each(data, function(index, array) {
$('.events').append(array['title']);
});
});
});
UPDATE: The problem has been resolved, turns out it was simply the fact that my Maven web project couldn't run the PHP files, so the PHP files did not return any data. After creating a PHP project, everything is working fine.