Note that there are some explanatory texts on larger screens.

plurals
  1. POPDOStatement: Getting different results between `fetchAll($mode);` and `setFetchMode($mode); fetchAll();`
    primarykey
    data
    text
    <p>I have a question regarding PDO:</p> <p>Is there a difference between </p> <pre><code>$pdo -&gt; fetchall(PDO::FETCH_GROUP|PDO::FETCH_ASSOC); </code></pre> <p>and</p> <pre><code>$pdo -&gt; setFetchMode(PDO::FETCH_GROUP|PDO::FETCH_ASSOC); $pdo -&gt; fetchall(); </code></pre> <p>Because I get a different result for them... The documentation from PHP about these fetchmodes is not very clear to me.</p> <p>Basically, I have a table with different pages, and I want to fetch all the pages indexed by their ID...</p> <p>colums of the table: id, page_name, lang_id, ...</p> <p>The first method returns this: </p> <pre><code>array { [id of page 1] =&gt; array { [0] =&gt; array { page 1 } } [id of page 2] =&gt; array { [0] =&gt; array { page 2 } } } </code></pre> <p>When I do the second method, I only get</p> <pre><code>array { [0] =&gt; { page 1 } [1] =&gt; { page 2 } [3] =&gt; { page 3 } ... } </code></pre> <p>This is clearly not what I wanted since I want it like this:</p> <pre><code>array { [id of page 1] =&gt; { page 1 } [id of page 2] =&gt; { page 2 } [id of page 3] =&gt; { page 3 } } </code></pre> <p>The first one is good enough for me because I can easily tweak it with the array map function: </p> <pre><code>array_map('reset', $arr); </code></pre> <p>EDIT My exact code:</p> <p>This is what they have in common:</p> <pre><code> $sql = "SELECT * FROM pages"; $q = $this -&gt; db -&gt; query($sql); </code></pre> <p>Method 1:</p> <pre><code> $result1 = $q -&gt; fetchall(PDO::FETCH_GROUP|PDO::FETCH_ASSOC); </code></pre> <p>Method 2:</p> <pre><code>$q -&gt; setFetchMode(PDO::FETCH_GROUP|PDO::FETCH_ASSOC); $result2 = $q -&gt; fetchall(); </code></pre> <p>Now when I do a var_dump on both the results I get a different output...</p>
    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.
    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