0

I want to use the following code to populate a drop-down list with all of the customer types:

<select name="type" id="type" class="neutral">
<?php   // SQL QUERY TO RETRIEVE EVERY TYPE OF CUSTOMER
$sql = "SELECT CUST_TYPE FROM `CUSTOMER` GROUP BY `CUST_TYPE`";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)){echo '<option value="'.$row.'">'.$row.'</option>';}
                ?>

The query works in phpMyAdmin; it gives the correct output (corporate, other, school, sports) but in the webpage it displays a drop-down list with 4 options, all containing the word "array." Please help!

4
  • print_r($row) to see what is in there -- it is a 2D array, so you need $row['CUST_TYPE'] Commented Nov 30, 2012 at 14:59
  • Holy cow! Yup, that was it :) Good job! Commented Nov 30, 2012 at 15:02
  • A side note about your query - you are not using any aggregate functions and so although your GROUP BY probably gives you the result you expect, more appropriate would be to SELECT DISTINCT CUST_TYPE FROM CUSTOMER Commented Nov 30, 2012 at 15:06
  • Thank you very much Michael Berkowski! Commented Nov 30, 2012 at 23:11

1 Answer 1

2

Try

while($row = mysql_fetch_assoc($result)){
    echo '<option value="'.$row['CUST_TYPE'].'">'.$row['CUST_TYPE'].'</option>';
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.