0

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 ♥

    amdixon
    • 3,814
    • 8
    • 25
    • 34
    Mr Akheel
    • 9
    • 2
    • You're echoing out `subcat` but your keys are `subcat1` and `subcat2` –  Oct 05 '15 at 05:39
    • Please read: http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php –  Oct 05 '15 at 05:42

    2 Answers2

    1

    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 } ?>
    
    Niranjan N Raju
    • 12,047
    • 4
    • 22
    • 41
  • you want to put condition to remove blank `
  • `??
  • – Niranjan N Raju Oct 05 '15 at 04:53
  • is it ok?? @NiranjanNRaju , as i think this column should be ckecked with empty since it will contain either string or emty string never going to be contain bool .. – Rohit Kumar Oct 05 '15 at 05:16
  • at any cost, even the column is empty or null, we will get that index from query result, so it should work fine.. – Niranjan N Raju Oct 05 '15 at 05:21