Note that there are some explanatory texts on larger screens.

plurals
  1. POUnify Array For JSON Encode?
    primarykey
    data
    text
    <p>I'm trying to json_encode my array I'm getting back so I can place it inside of a jQuery plugin for the data source. My issue is that I am using PDO to query my database to get back the array, but upon doing a print_r, I see that every name I'm getting back from my database is in its own array. How would I place all these results into a single array so that when I do my json_encode it is all in one readable string for the jQuery plugin?</p> <p>Database Query (PDO) - </p> <pre><code>$query = " SELECT name FROM `clients` "; try { $stmt = $b3-&gt;prepare($query); $stmt-&gt;execute(); } catch(PDOException $ex) { die("Failed to run query: " . $ex-&gt;getMessage()); } $players = $stmt-&gt;fetchAll(); </code></pre> <p>Example of the returned array upon doing (<em>Note I have 8000 getting returned so I will only post the first 5 or so</em>)</p> <pre><code>print_r($players); Array ( [0] =&gt; Array ( [name] =&gt; ! CBC.ZXR ) [1] =&gt; Array ( [name] =&gt; ! marioxz ) [2] =&gt; Array ( [name] =&gt; ! V v :] ) [3] =&gt; Array ( [name] =&gt; !?! ) [4] =&gt; Array ( [name] =&gt; !CU @ 1337 ) </code></pre> <p>So more or less, how would I unify my array, so that when I do</p> <pre><code>json_encode($players["name"]); </code></pre> <p>It will return back a single JSON string of the names mentioned above.</p> <p><strong>Edit</strong></p> <p>Current Code, </p> <pre><code>$query = " SELECT name FROM `clients` "; try { $stmt = $b3-&gt;prepare($query); $stmt-&gt;execute(); } catch(PDOException $ex) { die("Failed to run query: " . $ex-&gt;getMessage()); } $playerNames = $stmt-&gt;fetchAll(PDO::FETCH_COLUMN, 0); json_encode($playerNames); var_dump(json_last_error()); </code></pre>
    singulars
    1. This table or related slice is empty.
    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