Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP Array not Formatting Correctly
    primarykey
    data
    text
    <p>I can't seem to figure out what the problem is, but my PHP array isn't outputting as proper JSON. I removed the json_encode function, only to find out that my array's are cascading and not properly formatting themselves. Here is an example of the output (with json_encode removed):</p> <pre><code>Array ( [0] =&gt; Array ( [username] =&gt; g4m3b0y [profile_photo] =&gt; default.png [email] =&gt; michael@me.com [first_name] =&gt; Michael [last_name] =&gt; Simpson ) ) Array ( [0] =&gt; Array ( [username] =&gt; g4m3b0y [profile_photo] =&gt; default.png [email] =&gt; michael@me.com [first_name] =&gt; Michael [last_name] =&gt; Simpson ) [1] =&gt; Array ( [username] =&gt; michelle [profile_photo] =&gt; default.png [email] =&gt; michelle@gmail.com [first_name] =&gt; Michelle [last_name] =&gt; Houston ) ) </code></pre> <p>As you can see, it's creating duplicate results in separate arrays, hence my issue with the JSON output. Here is my code which is generating the results:</p> <pre><code> $sql2 = "SELECT DISTINCT username, profile_photo, first_name, last_name, email FROM users WHERE ( first_name LIKE '%$q%' OR last_name LIKE '%$q%' ) ORDER BY last_name"; $sq2 = mysql_query($sql2); $st2 = mysql_num_rows($sq2); if($st2&gt;=1) { while($a = mysql_fetch_array($sq2)) { $userRow[] = array( 'username' =&gt; $a['username'], 'profile_photo' =&gt; $a['profile_photo'], 'email' =&gt; $a['email'], 'first_name' =&gt; $a['first_name'], 'last_name' =&gt; $a['last_name'] ); header('Content-Type: application/json'); echo json_encode($userRow); } } else { exit; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload