Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Fetch as assoc</p> <p>For the manual: <a href="http://php.net/manual/en/pdostatement.fetchall.php">http://php.net/manual/en/pdostatement.fetchall.php</a></p> <p>fetch_style</p> <blockquote> <p>Controls the contents of the returned array as documented in PDOStatement::fetch(). Defaults to value of PDO::ATTR_DEFAULT_FETCH_MODE (which defaults to PDO::FETCH_BOTH)</p> <p>To return an array consisting of all values of a single column from the result set, specify PDO::FETCH_COLUMN. You can specify which column you want with the column-index parameter.</p> <p>To fetch only the unique values of a single column from the result set, bitwise-OR PDO::FETCH_COLUMN with PDO::FETCH_UNIQUE.</p> <p>To return an associative array grouped by the values of a specified column, bitwise-OR PDO::FETCH_COLUMN with PDO::FETCH_GROUP.</p> </blockquote> <p>That last bit is key. It doesn't seem to be completely documented (that I could find), but instead of PDO::FETCH_COLUMN, you can combine PDO::FETCH_ASSOC with PDO::FETCH_GROUP to achieve the desired result:</p> <pre><code>$PDOstmt-&gt;fetchAll(PDO::FETCH_ASSOC | PDO::FETCH_GROUP) </code></pre> <p>So, given the above data:</p> <pre><code>$stmt = $PDO_obj-&gt;prepare('select * from brands'); $stmt-&gt;execute(); $result = $stmt-&gt;fetchAll(PDO::FETCH_ASSOC | PDO::FETCH_GROUP); d($result); </code></pre> <p>Results in:</p> <pre><code>array (6) [ '1' =&gt; array (1) [ array (2) [ 'name' =&gt; string (10) "Solidfloor" 'url' =&gt; string (10) "solidfloor" ] ] '2' =&gt; array (1) [ array (2) [ 'name' =&gt; string (9) "Quickstep" 'url' =&gt; string (9) "quickstep" ] ] '4' =&gt; array (1) [ array (2) [ 'name' =&gt; string (10) "Cleanfloor" 'url' =&gt; string (10) "cleanfloor" ] ] '5' =&gt; array (1) [ array (2) [ 'name' =&gt; string (12) "Blue Dolphin" 'url' =&gt; string (12) "blue-dolphin" ] ] '6' =&gt; array (1) [ array (2) [ 'name' =&gt; string (5) "Krono" 'url' =&gt; string (5) "krono" ] ] '8' =&gt; array (1) [ array (2) [ 'name' =&gt; string (7) "Meister" 'url' =&gt; string (7) "meister" ] ] ] </code></pre> <p>( d() is just a handy debugging function from the kint library, like var_dump() or print_r() )</p> <p>Note that the column used to index the array will always be the first column in the results, so you can modify your select statement to choose which column you want. And note also that the indexed column will be stripped out of each row's array; to get around that, you can add the column twice to your select statement (i.e., <code>select id, brands.* from brands</code>, etc.).</p> <p>There are more parameters documented here: <a href="http://php.net/manual/en/pdostatement.fetch.php">http://php.net/manual/en/pdostatement.fetch.php</a> , like PDO::FETCH_UNIQUE to make sure that each index is used only once.</p>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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