简体   繁体   中英

Select colum1, column2 from table where value=somevalue

I want to select two columns value and use them with while, i tried but failed. new to php&Mysql..

Mytable = categories ↓

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

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

My Query:

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>

Output I want to get :

  • High School
  • Pvt School
  • Jr College
  • Voc College
  • Please Help me out.. ♥ Hearty Thanks ♥

    echo both values inside while loop

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

    Dour your stuff like below-

    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 ?>
    

    OR better to modify your query only as

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

    The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM