0

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>

1 Answer 1

1

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>";}}
  }
Sign up to request clarification or add additional context in comments.

3 Comments

Did you test this code? It's not working either. The dropdown list is empty. @Michael Moxley
It should work, it's copy / pasted from a working form of mine. I know this is simple, but is the table and SQL correct?
Yeah I just double checked the database names and the table. No idea what's wrong @Michael Moxley -_-

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.