Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have made input select tab using mysql database.

Using While:

<select name="membership_ID" id="membership" tabindex="1" required="required">
    <option value="">-- Membership Type --</option>
    <?php $get_membership = mysql_query("SELECT * FROM membership");
    while($row_membership = mysql_fetch_array($get_membership, MYSQL_ASSOC)): ?>
        <option value="<?php echo $row_membership['ID']; ?>"> <?php echo $row_membership['name']; ?> </option>
    <?php endwhile; ?>
</select>

using For loop:

<?php $get_membership = mysql_query("SELECT * FROM membership");
    $numRows = mysql_num_rows($get_membership);
    for($i=1;  $i<= $numRows; $i++){
        $row_membership = mysql_fetch_array($get_membership, MYSQL_ASSOC) ?>
        <option value="<?php echo $row_membership['membership_ID']; ?>"> <?php echo $row_membership['membership_name']." - ".$row_membership['membership_price']." Rs"; ?> </option>
<?php } ?>

this works fine and gives values placed as options when no while loop is there. But when while/for loop places, options in while/for loop are not displayed. Note: table membership contains data.

share|improve this question
2  
First, you should not be using mysql_* methods as they are deprecated, use mysqli or PDO instead. Second you are doing nothing in your code to handle any error conditions from the database. Add this so that you can clearly understand what errors you might be getting. Third, you should probably do a loop earlier in you code to where you don't have this spaghetti code. Why even try to output a select element at all if your query failed? – Mike Brant Jan 29 at 0:36
add comment (requires an account with 50 reputation)

1 Answer

Try this :

$row_membership = mysql_fetch_array($get_membership) ?>
share|improve this answer
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.