I have used the code below to extract the data from my database and convert it into an array. My problem is that the echo json_encode function does not work and when running this code (without the print_r function) I am left with a blank page.
$query = "SELECT * From table";
$resultarray = array();
if ($result = mysqli_query($connection, $query)) {
while ($row = mysqli_fetch_row($result)) {
$resultarray[] = $row;
}
print_r($resultarray); // This line shows that the array is works but the code below does not convert to JSON.
echo json_encode($resultarray);
}
I have used the print_r function to make sure that I have created an array within my code. I have gone around in circles for hours and I do not understand what I am doing wrong. If I use the print_r function and view the page source I get the following:
Array
(
[0] => Array
(
[0] => 5
[1] => Name 1
[2] => Description 1
[3] => Location 1
)
[1] => Array
(
[0] => 6
[1] => Name 2
[2] => Description 2
[3] => Location 2
)
[2] => Array
(
[0] => 45
[1] => Name 3
[2] => Description 3
[3] => Location 3
)
Thanks.