How to pass data from the database to combobox?

1

I wanted to put some data from the database in a combobox but I can not, I think the problem will be the connection to the database but I do not know what will be wrong, can you help me?

<?php
...
$dbconn = mysqli_connect($servername, $username, $password, $dbname)or die("Failed to connect to database:" . mysqli_error($dbconn));

$query = "SELECT nome FROM Medicos";
$data = mysqli_query($dbconn, $query);
$result = mysqli_num_rows($data);       

?>
  <form name="medicos" method="post" action="">
  <label for="">Selecione um m&eacute;dico</label>
  <select>
  <option>Selecione...</option>

<?php while($prod = $result->fetch_assoc()) { 
   echo '<option value="'.$prod['nome'].'">'.$prod['nome'].'</option>';
   }
?>    
</select>
    
asked by anonymous 23.06.2017 / 15:35

1 answer

1

Do your while in the variable $data because it is the one that executed the Query

<?php

...
$dbconn = mysqli_connect($servername, $username, $password, $dbname)or die("Failed to connect to database:" . mysqli_error($dbconn));

$query = "SELECT nome FROM Medicos";
$data = mysqli_query($dbconn, $query);
$result = mysqli_num_rows($data);       

?>
  <form name="medicos" method="post" action="">
  <label for="">Selecione um m&eacute;dico</label>
  <select>
  <option>Selecione...</option>

<?php while($prod = $data->fetch_assoc()) { 
   echo '<option value="'.$prod['nome'].'">'.$prod['nome'].'</option>';
   }
?>    
</select>
    
23.06.2017 / 15:38