Ryan Oscar
Ryan Oscar

Reputation: 279

Fetching data to HTML dropdown list to from database

I need to fetch user's company type to dropdown list from database. According to my coding data is not fetching properly.I tried to print the result but even it's not outputting anything. Database variable name is 'type'. Could someone give any help?

<select name="type" class="form-control">
              <?php


mysql_connect('localhost', 'root', '123');
mysql_select_db('db1');

$sql= "SELECT type FROM users";
$result= mysql_query($sql);
echo "$result";

    while($row= mysql_fetch_array($result))
    {
      echo "<option value='". $row['type'] ."'>" .$row['type'] ."</option>" ;
    }


?>

 </select>

Upvotes: 0

Views: 85

Answers (1)

Michael Moxley
Michael Moxley

Reputation: 36

Here is what I use on my forms (modified for your terms).

    mysql_connect('localhost', 'root', '123');
    mysql_select_db('db1');
    $sql = "SELECT type FROM users";
    $result = mysql_query($sql) or die ("Error in query: $sql. " . mysql_error());
    if (mysql_num_rows($result) > 0){
       while($row = mysql_fetch_object($result)){
          echo "<option value=\"".$row->type."\">".$row->type."</option>";}}
  }

Upvotes: 1

Related Questions