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

Asked

Viewed 61 times

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 will make 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 one $qry_result table without knowing what will be returned?

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

1 answer

2


To show the results, just this:

<?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. You can think of a DROP TABLE up to a query to your password table.


Title version:

<?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";
   }
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.