0

I want to populate a html drop down menu with a list query from database. I have the following...

<?php
$conn = oci_connect(//connection stuff goes here//);
$stid = oci_parse($conn, "SELECT DESCRIPTION FROM COUNTRY WHERE COUNTRY_ID IS NOT NULL");

$result = oci_execute($stid, OCI_DEFAULT);



echo '<select>';

while ($row = oci_fetch_array($result)) {
echo '<option value=' . $row['DESCRIPTION'] . '</option>';
}
echo '</select>';

?>

Something to with the qoute marks on the echo line? Any help here would be great :)

2 Answers 2

1

This question has nothing to do with Oracle. Just replace this:

echo '<option value=' . $row['DESCRIPTION'] . '</option>';

... with this:

echo '<option>' . htmlspecialchars($row['DESCRIPTION']) . '</option>';

You were basically generating this:

<option value=Blah Blah Blah</option>

Edit: And you forgot about COUNTRY_ID! Add it to the SELECT statement and use it:

echo '<option value="' . htmlspecialchars($row['COUNTRY_ID']) . '">' . htmlspecialchars($row['DESCRIPTION']) . '</option>';
2
  • Sorry but this has not fixed it either :( PHP returns no errors but on render drop down is not populated with options :/ Mar 26, 2013 at 18:14
  • @user1214840 - That's basic debugging we cannot do for you. Get familiar with var_dump(), print_r() and your browser's "View Source" menu item. Mar 27, 2013 at 8:09
0

Yes, your assumption was right, you have to enclose your description in double quotes and also insert it inside the tags, like this:

echo '<option value="' . $row['DESCRIPTION'] . '">'.$row['DESCRIPTION'].'</option>';
2
  • 1
    You need to use htmlspecialchars() when inserting arbitrary strings into HTML.
    – Brad
    Mar 26, 2013 at 17:56
  • Thanks, but still no luck. Drop down menu appears but is not populated with any rows. Something wrong with my query perhaps? Mar 26, 2013 at 18:01

Your Answer

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

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