PHP- Show query results in a select

1

I made an input for admin to write a message to a specific user. The user will be selected by a select who will fetch the users table from the names and list them all. After selecting someone, the admin can write the message, which would be saved in the bd with the "user" field equal to the one previously selected.

$resultado = mysqli_query($ligacao, $sql);
while ($row = mysqli_fetch_assoc($result)) {
    $rows[] = $row;
}
return $rows;

$rows = db_select("SELECT name FROM utilizadores");
if ($rows === false) {
    $error = db_error();
    echo $rows;
    // Handle error - inform administrator, log to file, show error page, etc.
}

I am not able to do the echo so I suppose the query is badly done, and if it is not, how clever does the result not select? I suppose it's inside the 'while' loop type:

    while ($row = mysqli_fetch_assoc($result)) {
        $rows[] = $row;
        ?>
       <select>
            <option>$rows[]</option>
       </select> 
       <?php
    }
    
asked by anonymous 09.05.2015 / 22:09

1 answer

2

You need to print with the current query item ($ row) and not the entire array ($ rows).

while ($row = mysqli_fetch_assoc($result)) {
    $rows[] = $row;
    ?>

<option>
   <?php echo $row['name'];?>
</option>

If the value displayed in the option is the same as the one that will be sent, you can omit the value attribute of the option.

Another way to mount a select

    
09.05.2015 / 22:26