title: Sql query returning different results in php from those in mysql shell
body: <p>This question is originates from an <a href="https://stackoverflow.com/questions/38308273/inserting-data-into-a-mysql-table-dynamically">earlier question</a>. </p>
  
  <p>When i run this script  on mysql shell (see below)</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>mysql&gt; SELECT 
      -&gt;     t1.post_id,
      -&gt;     'multiple' AS multiple,
      -&gt;     COUNT(*) as count
      -&gt; FROM people_publication AS t1 GROUP BY t1.post_id
      -&gt; UNION
      -&gt; SELECT
      -&gt;     t2.post_id,
      -&gt;     REPLACE(
      -&gt;         CONCAT(
      -&gt;             'multiple_',
      -&gt;             @curRow:=CASE
      -&gt;                 WHEN @postId = t2.post_id THEN @curRow + 1
      -&gt;                 ELSE 0
      -&gt;             END,
      -&gt;             @postId:=t2.post_id
      -&gt;         ),
      -&gt;         t2.post_id,
      -&gt;         ''
      -&gt;     ) AS multiple,
      -&gt;     t2.pid
      -&gt; FROM people_publication AS t2
      -&gt; ORDER BY post_id, multiple;
  +---------+------------+-------+
  | post_id | multiple   | count |
  +---------+------------+-------+
  |    7209 | multiple   |     3 |
  |    7209 | multiple_0 |    27 |
  |    7209 | multiple_1 |    34 |
  |    7209 | multiple_2 |     1 |
  |    7210 | multiple   |     3 |
  |    7210 | multiple_0 |    51 |
  |    7210 | multiple_1 |    11 |
  |    7210 | multiple_2 |    37 |
  |    7211 | multiple   |     1 |
  |    7211 | multiple_0 |    36 |
  |    7212 | multiple   |     1 |
  |    7212 | multiple_0 |    11 |
  |    7213 | multiple   |     1 |
  |    7213 | multiple_0 |    15 |
  |    7215 | multiple   |     1 |
  |    7215 | multiple_0 |    42 |
  +---------+------------+-------+
  16 rows in set (0.00 sec)</code></pre>
  </div>
  </div>
  
   i get different results compared when i run the same script with php</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>$query = "SELECT 
      t1.post_id,
      'multiple' AS multiple,
      COUNT(*) as count
  FROM people_publication AS t1 GROUP BY t1.post_id
  UNION
  SELECT
      t2.post_id,
      REPLACE(
          CONCAT(
              'multiple_',
              @curRow:=CASE
                  WHEN @postId = t2.post_id THEN @curRow + 1
                  ELSE 0
              END,
              @postId:=t2.post_id
          ),
          t2.post_id,
          ''
      ) AS multiple,
      t2.pid
  FROM people_publication AS t2
  ORDER BY post_id, multiple";
  
  $sql=$con-&gt;prepare($query);
  $sql-&gt;execute();
  $sql-&gt;setFetchMode(PDO::FETCH_ASSOC);
   while($row=$sql-&gt;fetch()){
    echo "&lt;pre&gt;";print_r($row);
          }
          
          </code></pre>
  </div>
  </div>
  
  which returns a table that looks like this (see below)</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>+---------+---------+------------+------------+
  | meta_id | post_id | meta_key   | meta_value |
  +---------+---------+------------+------------+
  |     816 |    7209 | multiple   | 3          |
  |     817 |    7209 | multiple_0 | 34         |
  |     818 |    7209 | multiple_0 | 1          |
  |     819 |    7209 | multiple_0 | 27         |
  |     820 |    7210 | multiple   | 3          |
  |     821 |    7210 | multiple_0 | 37         |
  |     822 |    7210 | multiple_0 | 51         |
  |     823 |    7210 | multiple_0 | 11         |
  |     824 |    7211 | multiple   | 1          |
  |     825 |    7211 | multiple_0 | 36         |
  |     826 |    7212 | multiple   | 1          |
  |     827 |    7212 | multiple_0 | 11         |
  |     828 |    7213 | multiple   | 1          |
  |     829 |    7213 | multiple_0 | 15         |
  |     830 |    7215 | multiple   | 1          |
  |     831 |    7215 | multiple_0 | 42         |
  +---------+---------+------------+------------+</code></pre>
  </div>
  </div>
  
  What causes this to happen?</p>
  
  <p>The connection file looks like this</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;?php
  //session_start();
  function dbConnect() {
    $host = 'localhost';
    $dbname = 'mydb';
    $user = 'root';
    $pass = '';
      try {
      $con= new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
  return $con;
  $con-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      } 
  
  catch (PDOException $e) {
        echo 'Cannot connect to database';
        exit();
      }
    
  }?&gt;</code></pre>
  </div>
  </div>
  </p>