title: Dynamically display row values as columns in HTML table
body: <p>I'm trying to make a query that dynamically displays row values as columns in an HTML table. I can get the query to work in phpMyAdmin. It does exactly what I want, but I don't know how I can present this in an HTML table on my PHP page.</p>
  <p>This is the code that works in phpMyAdmin:</p>
  <pre class="lang-sql prettyprint-override"><code>SET @sql = NULL;
  SELECT
    GROUP_CONCAT(DISTINCT
      CONCAT(
        'max(case when field_key = ''',
        field_key,
        ''' then field_value end) ',
        field_key
      )
    ) INTO @sql
  FROM
    Meeting;
  SET @sql = CONCAT('SELECT Meeting_id, ', @sql, ' 
                    FROM Meeting 
                     GROUP BY Meeting_id');
  
  PREPARE stmt FROM @sql;
  EXECUTE stmt;
  DEALLOCATE PREPARE stmt;
  </code></pre>
  <p>I changed it to this to try to make it work in PHP</p>
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  $mysqli = new mysqli(&quot;localhost&quot;, &quot;DB-USER&quot;, &quot;DB-PASS&quot;, &quot;DB-NAME&quot;);
  
  /* check connection */
  if (mysqli_connect_errno()) {
      printf(&quot;Connect failed: %s\n&quot;, mysqli_connect_error());
      exit();
  }
  
  $query = &quot;SET @sql = NULL;
  SELECT
    GROUP_CONCAT(DISTINCT
      CONCAT(
        'max(case when name = ''',
        name,
        ''' then value end) ',
        name
      )
    ) INTO @sql
  FROM
    TBL-NAME;
  SET @sql = CONCAT('SELECT data_id, ', @sql, ' 
                    FROM TBL-NAME 
                     GROUP BY data_id')&quot;
  
  if ($stmt = $mysqli-&gt;prepare($query)) {
  
      /* execute statement */
      $stmt-&gt;execute();
  
      /* bind result variables */
      /* NO CLUE WHAT TO DO WITH THIS */
      $stmt-&gt;bind_result($name, $code);
  
      /* fetch values */
      while ($stmt-&gt;fetch()) {
          printf (&quot;%s (%s)\n&quot;, $name, $code);
      }
  
      /* close statement */
      $stmt-&gt;close();
  }
  
  /* close connection */
  $mysqli-&gt;close();
  ?&gt;
  </code></pre>
  <p>Also tried fiddling arround with some examples I found like below.</p>
  <pre class="lang-php prettyprint-override"><code>if ($sql-&gt;num_rows &gt; 0) {
      // output data of each row
      while($row = $result-&gt;fetch_assoc()) {
          echo $row. &quot;&lt;br&gt;&quot;;
      }
  </code></pre>
  <p>Cannot get it to work. Anyone willing to give me some directions?
  What I want to achieve is to have user values presented in a table on one line per user.</p>