fill in combobox with bank data

I'm following a video class of php with mysql and I'm able to list the data of a table in a list, just so you know that the problem of connection with the bank is already overcome. I tried to use the same code to list the same table in a combobox, and it's not filling. Combo appears, but does not fill with the data. Does anyone know where I'm going wrong? Following code:

<body>
    <ul>
        <?php
            // Passo 4 - Listagem dos dados
            while($registro = mysqli_fetch_assoc($categorias)){
        ?>
                <li><?php echo $registro ["nomecategoria"]?></li>
        <?php
            }

        ?>
    </ul>
    <form name="produto" method="post" action="">
         <label for="">Selecione um produto</label>
         <select>
         <option>Selecione...</option>

         <?php 
         while($registro = mysqli_fetch_assoc($categorias)) { ?>
         <option = "<?php echo $registro['categoriaID'] ?>"><?php echo $registro["nomecategoria"] ?></option>
         <?php } ?>

         </select>
    </form>
                <?php
        //Passo 5 - Liberar dados da memória
        mysqli_free_result($categorias);
    ?>
</body>
Author: Leo Caracciolo, 2018-06-06

2 answers

True. It was time to copy that he jumped. But I want to emphasize that the consultation to the bank is all right, because the list above combo is filling normally.

 <body>
    <ul>
        <?php
            // Passo 4 - Listagem dos dados
            while($registro = mysqli_fetch_assoc($categorias)){
        ?>
                <li><?php echo $registro ["nomecategoria"]?></li>
        <?php
            }

        ?>
    </ul>
    <form name="produto" method="post" action="">
         <label for="">Selecione um produto</label>
         <select>
         <option>Selecione...</option>

         <?php 
         while($registro = mysqli_fetch_assoc($categorias)) { ?>
         <option = "<?php echo $registro['categoriaID'] ?>"><?php echo $registro["nomecategoria"] ?></option>
         <?php } ?>

         </select>
    </form>
                <?php
        //Passo 5 - Liberar dados da memória
        mysqli_free_result($categorias);
    ?>
</body>
 0
Author: Neto Sales, 2018-06-06 17:40:55

It happens that fetch_array /fetch_assoc "consumes" all rows in the first while, i.e. changes the internal pointer to the end of the data during while. In this way you need to return the ponteiro to the first row by putting mysqli_data_seek($categorias, 0); before the second while

..................
..................
<form name="produto" method="post" action="">
     <label for="">Selecione um produto</label>
     <select>
     <option>Selecione...</option>

     <?php 
     mysqli_data_seek($categorias, 0);
     while($registro = mysqli_fetch_assoc($categorias)) { ?>
     <option = "<?php echo $registro['categoriaID'] ?>"><?php echo $registro["nomecategoria"] ?></option>
     // o correto acima é <option VALUE = "<?php ......
     // FALTOU O value

     .................
     .................

Mysqli_data_seek

You could also use only one while to get the same result:

<ul>
    <?php
        // Passo 4 - Listagem dos dados
        while($registro = mysqli_fetch_assoc($categorias)){

            echo "<li>". $registro ["nomecategoria"] . "</li>";
            //concatena os registros retornados
            $paraSelect .= "<option value='" . $registro["categoriaID"] ."'>" .$registro["nomecategoria"] . "</option>";

        }

    ?>
</ul>
<form name="produto" method="post" action="">
     <label for="">Selecione um produto</label>
     <select>
     <option>Selecione...</option>

     <?php 
      //imprime os options
      echo $paraSelect; 
     ?>

     </select>
</form>
<?php
    //Passo 5 - Liberar dados da memória
    mysqli_free_result($categorias);
?>
 0
Author: Leo Caracciolo, 2018-06-06 18:44:01