PHP- Display query results in a select

Asked

Viewed 970 times

1

I input an admin to write a message to a specific user. The user will be selected by a select that fetches the names from the user table and lists them all. After selecting someone, the admin can write the message, which would be saved in the comic with the "user" field that was 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’m not doing echo so I guess the query is wrong, and if not, how do I list the result in select? I suppose it’s inside the while loop like:

    while ($row = mysqli_fetch_assoc($result)) {
        $rows[] = $row;
        ?>
       <select>
            <option>$rows[]</option>
       </select> 
       <?php
    }

1 answer

2

You need to imprint with echo the current query item($Row) and not the full 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 sent, you can omit the option value attribute.

Another way to mount a select

  • thanks! I’ve already made the select.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.