php - populate select dropdown menu with category names from mysql database -
this question has answer here:
hi im trying populate select dropdown menu names in database mysql, im getting blank dropdown nothing in , not expanding. in advacnce
database: icadbs504a
create table if not exists `category` ( `cat_id` int(11) not null, `cat_name` varchar(40) not null, `cat_path` varchar(20) not null ) engine=innodb default charset=utf8 auto_increment=6 ; insert `category` (`cat_id`, `cat_name`, `cat_path`) values (1, 'computers', 'uploads/1/'), (2, 'phones', 'uploads/2/'), (3, 'toys', 'uploads/3/'), (4, 'pet accessories', 'uploads/4/'), (5, 'camping', 'uploads/5/');
form:
<select name="category"> <?php // make connection: $dbc = mysqli_connect ('localhost', 'root', 'password', 'icadbs504a'); // if no connection made, trigger error: if ($dbc->connect_error) { die("database selection failed: " . $dbc->connect_error); } # set encoding match php script encoding. mysqli_set_charset($dbc, 'utf8'); $q = "select cat_id, cat_name category ". "order cat_name"; $r = mysqli_query($q); while($row = mysqli_fetch_array($r)) { echo "<option value=\"".$row['cat_id']."\">".$row['cat_name']."</option>\n "; } ?> </select>
as per op's wish close question:
you need pass db connection query.
either using:
$r = $dbc->query($q);
or:
$r = mysqli_query($dbc,$q);
- the choice yours.
Comments
Post a Comment