Rajeev Kumar
Rajeev Kumar

Reputation: 7

How to populate a particular column list in Combo Box with PHP?

I am using the given code below to populate some values from a column of a table. It's just getting filled blank..

Can you please find the problem with it ?

<select name="category">
    <option value="" selected>Select a category</option>
    <?php
        mysql_connect("localhost","root","");
        mysql_select_db("muskilaasaan");
        $category = "SELECT cat FROM category";
        $query_result = mysql_query($category);
        while($result = mysql_fetch_array($query_result))
        {
        ?>
            <option value = "<?php echo $result['cat']?>"/>
        <?php
        }

    ?>  
</select>

Upvotes: 0

Views: 2828

Answers (2)

Matthew Scragg
Matthew Scragg

Reputation: 4638

<select name="category">
    <option value="" selected>Select a category</option>
    <?php
        mysql_connect("localhost","root","");
        mysql_select_db("muskilaasaan");
        $category = "SELECT cat FROM category";
        $query_result = mysql_query($category);
        while($result = mysql_fetch_assoc($query_result))
        {
        ?>
            <option value = "<?php echo $result['cat']?>"><?php echo $result['cat']?></option>
        <?php
        }

    ?>  
</select>

Changed to mysql_fetch_assoc and also you didn't put anything in the option tags, that would cause it to appear blank.

Upvotes: 1

dom
dom

Reputation: 651

it does not help if you specify the connection string as a second argument in mysql_select_db() ? see here for an example: http://php.net/manual/de/function.mysql-select-db.php

Upvotes: 0

Related Questions