Hi

i m trying to get the values from database and put it into the select options..
i m using code-

<?php
$con = mysql_connect("localhost","cdccpl","d123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
					  mysql_select_db("cdccpl_aus", $con);
$result = mysql_query("SELECT * FROM bak_bill_cust");
 while( $row = mysql_fetch_array( $result ) ):
 $option = $row["cust_code"];

echo "<option value='$row[cust_code]'>$row[cust_code]</option>";
endwhile;
 mysql_close($con);?>

but i m getting displayes with the values as--

Customer auswidestonemanpnicholsonllwmplnewuserguestanftasturnbullanglic

means values getting displayed one after another and not in option..

why so..
plz help

Dani AI

Generated

Brief diagnosis and practical checks

When option values run together instead of appearing as dropdown items, the browser is not treating those outputs as option elements. That usually means they were either emitted outside a select/optgroup context or were HTML-escaped/altered before reaching the browser. As pointed out, validate the markup first — use the browser "View Source" and the DOM inspector to see exactly what the server sent and how the browser parsed it. The HTML option element requirements are documented here: HTML option element.

What to inspect, step by step

  • Confirm the raw HTML sent by PHP: if you see escaped entities (for example, &lt;option&gt;) then some output-escaping is running on those strings; search for templating/escaping calls or functions that encode HTML. See the PHP htmlspecialchars reference for correct escaping of values: htmlspecialchars.
  • If the option elements are present but not nested under a select, check for an unclosed tag earlier in the page or missing wrapper markup. A missing wrapper will cause browsers to render text inline instead of as list items.
  • Inspect DB values for stray markup, quotes, or control characters that could break attributes; trim and sanitize values before output.

Maintenance and safety notes

Prefer building the select markup in a controlled string (as suggested in the replies by and ) so it’s easier to insert a selected state and debug output. For modern PHP, stop using the old mysql_* API and use mysqli or PDO with prepared statements for security and better error handling: mysqli | PDO. During development enable error reporting and use the browser devtools to confirm the DOM state — that will quickly reveal whether the problem is HTML structure, escaping, or data content.

Recommended Answers

All 6 Replies

echo your option tags inside of a select tag like this:

<select name="test" id="test">
	<option value="value 1">option 1</option>
	<option value="value 2">option 2</option>
	<option value="value 3">option 3</option>
</select>

Well firstly

$result = mysql_query("SELECT * FROM bak_bill_cust");
 while( $row = mysql_fetch_array( $result ) ):
 $option = $row["cust_code"];

echo "<option value='$row[cust_code]'>$row[cust_code]</option>";
endwhile;

>>>

$result = mysql_query("SELECT * FROM bak_bill_cust");
while( $row = mysql_fetch_assoc( $result ) )
{
    $option = $row["cust_code"];
    echo "<option value='$option'>$option</option>";
}

I actually wasn't even aware PHP could use that format for while loops but I wouldn't advise its use given that it completely breaks the Perl-like syntax and just makes for ugly code.

I actually wasn't even aware PHP could use that format for while loops

Yeah.. I wasn't aware too.. :)

Not sure if your porblem is solved yet, but here is something I use all the time for drop downs.
First get your connection string going. preferably using an include.
then use the following code, adapting statement to your needs:

<?php
$result = mysql_query("SELECT * FROM users ORDER BY username ASC") or 
die(mysql_error());
	$sticky= '';
	if (isset($_POST['id']))
	$sticky = ($_POST['id']);
	$pulldown1 = '<select name="id">';
	$pulldown1 .= '<option></option>';
	while($row = mysql_fetch_array($result))
		{
		if($row['id'] == $sticky) {
		$pulldown1 .= "<option selected value=\"{$row['id']}\">
			{$row['username']}&nbsp;-&nbsp;
                                                {$row['id']}				
                                                </option>\n";
                                } else {
                                $pulldown1 .= "<option value=\"{$row['id']}\">					{$row['username']}&nbsp;-&nbsp;
                                                {$row['id']}
                                                </option>\n";
                                           }
                                }
                               $pulldown1 .= '</select>';
                                echo $pulldown1;	
            ?>

This works well for holding the users selected values when error checking as well...
hope this helps

Hi all..
thanks for reply..

Problem is solved..

try

$sql = "SELECT * FROM bak_bill_cust";
$query = mysql_query($sql) or die('Error: ' . mysql_error());
$select = '<select name="test">';
$select .= '<option value=""></option>';
while ($row = mysql_fetch_assoc($query)) {
$select .= '<option value="' . $row['cust_code'] . '">' . $row['cust_code'] . '</option>';
}
$select .= '</select>';
echo $select;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.