I am trying to load some events from a MySQL Database into a calendar from FullCalendar. For that, I have created a connection to the Database in a PHP file for getting the data and outputting it as a JSON.
The connection is working fine and when executing the PHP file in a terminal, I get a JSON with the correct data.
But when trying to get the data through the FullCalendar API, it doesn't work. In the browser console I get the Warning 'Failure parsing JSON' from FullCalendar and when looking at the response it tried to parse, it's just the php code itself, not its output.
So I copied the correct output I got from executing the PHP file in a normal terminal and replaced the PHP code in the PHP file with it, and then the parsing was successful, so apparently FullCalendar just tries to parse the contents of the PHP file instead of its output.
What is the reason for that? How do I correctly transfer the output from the PHP file to the FullCalendar API?
JavaScript in index.html:
<script>
document.addEventListener('DOMContentLoaded', function() {
var calendarEl = document.getElementById('calendar');
var calendar = new FullCalendar.Calendar(calendarEl, {
initialDate: '2021-09-12',
events: "get-events.php",
});
calendar.render();
});
</script>
get-events.php
<?php
$servername = "127.0.0.1";
$username = "root";
$password = "";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Title as title, Start as start, End as end FROM event_table";
$result = mysqli_query($conn, $sql);
$myArray = array();
if ($result -> num_rows > 0) {
while($row = $result -> fetch_assoc()) {
$myArray[] = $row;
}
}
print json_encode($myArray);
?>
The solution was simple, it had nothing to do with the PHP or JavaScript Code, my server environment just wasn't correctly configured (especially PHP), so the PHP code couldn't be executed.