How to list a query without knowing what will be returned?

2

In this example I defined some attributes to be shown, but in the game I do not know which ones to show because the user is the one who is going to ask the query.

<?php 
   while ($row = mysql_fetch_array($qry_result)){?>
   <tr>
     <td><?= $row[id_funcionario]?></td>
     <td><?= $row[end_funcionario]?></td>
     <td><?= $row[nome_funcionario]?></td>
   </tr>

<?php } ?>

How do I list this $ qry_result in the table without knowing what will be returned?

Example: In my SQL Game there is a field where the user types the SQL statement. It can type for example select * from funcionarios . How do I display the information in the table without knowing what it will type?

    
asked by anonymous 21.11.2014 / 00:56

1 answer

2

To show the results, this is enough:

<?php
   while ( $row = mysql_fetch_array($qry_result, MYSQL_NUM) ) {
      echo '<tr>';
      foreach($row as $item) {
         echo '<td>';
         echo htmlentities( $item );
         echo '</td>';
      }
      echo "</tr>\n";
   }
?>

Remembering that you need to limit user access privileges, otherwise it will do much more than give SELECT where you imagine it. You can think from a DROP TABLE to a query to your password table.


Version with titles:

<?php
   $primeira = true;
   while ( $row = mysql_fetch_array($qry_result, MYSQL_ASSOC) ) {
      if ($primeira ) {
         echo '<tr>';
         foreach($row as $titulo => $item) {
            echo '<th>';
            echo htmlentities( $titulo );
            echo '</th>';
         }
         echo '</tr>';
         $primeira = false;
      }
      echo '<tr>';
      foreach($row as $item) {
         echo '<td>';
         echo htmlentities( $item );
         echo '</td>';
      }
      echo "</tr>\n";
   }
?>
    
21.11.2014 / 01:09