Converting an array of items from SQL query into JSON using PHP
<p>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.  </p>

<pre><code>$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);
}
</code></pre>

<p>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:</p>

<pre><code> Array
(
[0] =&gt; Array
    (
        [0] =&gt; 5
        [1] =&gt; Name 1
        [2] =&gt; Description 1
        [3] =&gt; Location 1

    )

[1] =&gt; Array
    (
        [0] =&gt; 6
        [1] =&gt; Name 2
        [2] =&gt; Description 2
        [3] =&gt; Location 2

    )

[2] =&gt; Array
    (
        [0] =&gt; 45
        [1] =&gt; Name 3
        [2] =&gt; Description 3
        [3] =&gt; Location 3

    )
</code></pre>

<p>Thanks.</p>