仅显示查询表中存在的MySQL列

I have several MySQL tables that are dynamically generated into a html table one table at a time through the code below. However, the tables don't have the same columns. i.e. One table has a description column, whereas the other does not.

Is the following code the best way to have all the possible MySQL columns among the various tables in the script but only show the MySQL columns that exist for the selected table? I feel like I'm redundant by writing "isset" for every column. Thanks!

<?php
$query = " SELECT * FROM $tablename ";

$query_select = mysqli_query($con,$query);
while($row = mysqli_fetch_array($query_select)) {
?>

<table>
  <tr>

    <?php if(isset($row['name'])){ ?>
      <td><?php echo $row['name'];?></td>
    <?php } ?>

    <?php if(isset($row['description'])){ ?>
      <td><?php echo $row['description']?></td>
    <?php } ?>

  </tr>
</table>

You could just loop through the results. However, that would not perform any checks. Most likely, you'll have to do something like this, depending on what you're actually getting from the database.

<?php foreach ($row as $field): ?>
    <?php if ($field): ?>
        <td><?php echo $field; ?></td>
    <?php endif; ?>
<?php endforeach; ?>

Edit: In keeping in line with the comment you added above, you could simply remove the if clause.

not sure if this is what you need, but you can use indexes instead for the column instead of names:

<?php
   $query = " SELECT * FROM $tablename ";

    $query_select = mysqli_query($con,$query);
    while($row = mysqli_fetch_array($query_select)) {
      echo $row[0] ." ".$row[1]." ".$row[2]
    }
?>

mysql_fetch_array

possible formating:

<table> 
    <?php 
    $query = " SELECT * FROM $tablename ";
    $query_select = mysqli_query($con,$query);
    while($row = mysqli_fetch_array($query_select)) { ?>
         <tr>
         <?php for(var $i=0; $i < count($row); $i++){ 
              echo "<td>". $row[i] ."</td>";
         } ?>
         </tr>
    <?php } ?>
</table> 

You can foreach the array instead.

<?php
$query = " SELECT * FROM $tablename ";   

$query_select = mysqli_query($con,$query);
?>
<table>
<?php while($row = mysqli_fetch_array($query_select, MYSQLI_ASSOC)) { ?>
  <tr>
    <?php foreach($row as $key => $value) { ?>
      <td><?=$value?></td>
    <?php } //Endforeach ?>
  </tr>
<?php } //Endwhile ?>
</table>

If you need to print labels you can also use an associative array and an additional iteration to do that as well.

<?php
$query = " SELECT * FROM $tablename ";
$keys = array('name' => 'Name Label', 'description' => 'Description Label');    

$query_select = mysqli_query($con,$query);
$i = 0;
?>
<table>
<?php while($row = mysqli_fetch_array($query_select, MYSQLI_ASSOC)) { ?>
  <?php if($i == 0) { ?>
    <tr>
      <?php foreach($row as $key => $value) { ?>
        <td><b><?=$keys[$key]?></b></td>
      <?php } //Endforeach ?>
    </tr>
  <?php } $i++; //Endif ?>
  <tr>
    <?php foreach($row as $key => $value) { ?>
      <td><?=$value?></td>
    <?php } //Endforeach ?>
  </tr>
<?php } //Endwhile ?>
</table>

You might want to make your code adapt to the fields in the result set:

<?php
$result = mysqli_query($con,$query);
$fields = mysqli_fetch_fields($result);
$myaliases = array(
  'column_id' => 'id'
);
?>
<table>
<tr>
  <?php foreach ($fields as $field): ?>
    <th><?php echo $myaliases[$field->name] ?: $field->name; ?></th>
  <?php endforeach; ?>
</tr>
<?php while($row = mysqli_fetch_array($result)): ?>
<tr>
  <?php foreach ($fields as $field): ?>
    <td><?php echo $row[$field->name]; ?></td>
  <?php endforeach; ?>
</tr>
<?php endwhile; ?>
</table>

Re comments:

I've added code above to print a table row for column headings.

I've also included an example of mapping a field name column_id to a table heading id in the output. If I define no alias for a given column, it defaults to the original field name by using the PHP 5.3 operator ?:

You could alternatively define column aliases in your SQL query like SELECT column_id AS id ...

See http://www.php.net/manual/en/mysqli-result.fetch-fields.php