douyouzheng2209 2013-07-19 21:07
浏览 49

仅显示查询表中存在的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>
  • 写回答

4条回答 默认 最新

  • duangou2028 2013-07-19 21:15
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)