从表中选择colum1,column2,其中value = somevalue

时间:2015-10-05 04:45:13

标签: php html mysql while-loop

我想选择两列值并使用while,我试过但失败了。新手到php& Mysql ..

Mytable =类别↓

subcat1    |   subcat2   |   category  |
=======================================
High school| Pvt School  |   Education |
---------------------------------------
Jr College | Voc College |   Education |

=======================================

我的查询:

sql="select distinct subcat1, subcat2 from categories where category='Education'";
$category=mysql_query($sql);

    <?php
       while ($cat = mysql_fetch_assoc($category))
    {
    ?>

    <a href="Category.php"> <?php echo $cat['subcat']; ?></a>

我想得到的输出:

  

  • 高中
  • 私立学校
  • Jr College
  •   Voc学院
  • 请帮帮我..♥衷心的感谢♥

    2 个答案:

    答案 0 :(得分:1)

    在循环中回显两个值

    <?php
        while ($cat = mysql_fetch_assoc($category)) { ?>
            <?php if($cat['subcat1']) { ?>
                <a href="Category.php"> <?php echo $cat['subcat1']; ?></a>
            <?php } if($cat['subcat1']) { ?>
                <a href="Category.php"> <?php echo $cat['subcat2']; ?></a>
            <?php } ?>
    <?php } ?>
    

    答案 1 :(得分:0)

    像下面这样的东西 -

    while ($cat = mysql_fetch_assoc($category))
    {
        ?>
        <?php
            if($cat['subcat1']!="") {
            ?>
                <a href="Category.php"> <?php echo $cat['subcat1']; ?></a>
        <?php } ?>
        <?php
            if($cat['subcat2']!="") {
            ?>
                <a href="Category.php"> <?php echo $cat['subcat2']; ?></a>
         ?php } ?>
    <?php } //while close ?>
    

    最好仅将您的查询修改为

    sql="select distinct subcat1, subcat2 from categories 
         where category='Education' AND subcat1<>'' AND subcat2<>''";