Dear all

I want to populate a few drop down boxes using PHP. However i have tried many examples i found from the web but the select box does not get any values. Below i have one example i have tried.

**********************************************
<?php
$link = mysql_connect("localhost", "root", "password") or die("Could not connect: " . mysql_error());
mysql_select_db('db', $link) or die (mysql_error());

$query = "SELECT last_name FROM main_author order by last_name";

$result = mysql_query($query) or die("Couldnt' execute query.");
$num = mysql_numrows($result);
?>

<html>
<body>
<form name="test" id="test" method="post">
<select value="" size="1" name="test2">
<option>all</option>
<? $i=0; while ($i<$num)
{$authors=mysql_result($result,$i,"last_name");
echo "<option>$authors</option>";
$++; }
mysql_close(); ?>
</select>
</body>
</html>
********************************************

any suggestions would be more than appreciated

Many thnx

Cheers

Recommended Answers

All 2 Replies

Dear all

<?php
$link = mysql_connect("localhost", "root", "password") or die("Could not connect: " . mysql_error());
mysql_select_db('db', $link) or die (mysql_error());

$query = "SELECT last_name FROM main_author order by last_name";

$result = mysql_query($query) or die("Couldnt' execute query.");
$num = mysql_numrows($result);
?>

<html>
<body>
<form name="test" id="test" method="post">
<select value="" size="1" name="test2">
<option>all</option>
<? 

$i=0; while ($i<$num) {

$row = mysql_fetch_assoc($result);

echo "<option>$row['last_name']</option>";

$i++; }
mysql_close(); ?>
</select>
</body>
</html>
********************************************

Try that and see if it works
Just loop thru the result using the array and print them

StatiX

you also need

<option value="something">Something</option>

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.