First off I am new to PHP,
I am trying to display categories from a mysql db in a drop down menu.
for some reason it is not displaying the category options.

Need help with the code. Can anyone help me out.

"the db has two fields "id" & "category_name"

<?php
$host=""; // Host name 
$username=""; // Mysql username 
$password=""; // Mysql password 
$db_name="images"; // Database name 
$tbl_name="categories"; // Table name
 
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$sql = "SELECT id,category_name FROM categories ORDER BY category_name"; 

echo '<select name="category_name">'; 

foreach ($conn->query($sql) as $row) { 
    echo '<option value="'.$row['id'].'">'.$row['category_name'].'</option>'; 
} 

echo '</select>';
?>

try this

<?php
$host=""; // Host name 
$username=""; // Mysql username 
$password=""; // Mysql password 
$db_name="images"; // Database name 
$tbl_name="categories"; // Table name
 
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$sql = "SELECT id,category_name FROM categories ORDER BY category_name"; 

echo '<select name="category_name">'; 

foreach (mysql_fetch_assoc(mysql_query($sql)) as $row) { 
    echo '<option value="'.$row['id'].'">'.$row['category_name'].'</option>'; 
} 

echo '</select>';
?>

Try this code

<?php
$host=""; // Host name 
$username=""; // Mysql username 
$password=""; // Mysql password 
$db_name="images"; // Database name 
$tbl_name="categories"; // Table name
 
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$sql = mysql_query("SELECT id,category_name FROM categories ORDER BY category_name"); 
$row = mysql_fetch_array($sql);
?>
<select name="Categor_name">
<?php do{ ?>
<option value="<?php echo $row['id']; ?>"><?php echo $row['category_name']; ?> </option>
<?php } while($row = mysql_fetch_array($sql));?>
</select>

hello try this:

#
while(mysql_fetch_assoc(mysql_query($sql)) )) {
echo '<option value="'.$row['id'].'">'.$row['category_name'].'</option>';

}

Thank you for all your input, I will have to try some of these. I'll let ya know how it goes. Thanks Again.

Hey Thanks again to everyone, It's up and running. You guys were a life saver.

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.