Create a list from an SQL database using HTML / PHP

I am trying to create a drop down list / menu of users from my SQL database table. Can someone help me with the code please? The code below is intended only for a regular list with parameters 1 and 2. How to make a list to get all the elements in a specific table using html / php / sql. Sorry, I'm not very experienced with this, thanks in advance.

<select name="user" id="user">
<option value="a" selected="selected">1</option>
<option value="b">2</option>
</select>
+3
source share
2 answers

there are several ways, but I will show my

First, take the data you want to retrieve from the query:

$query = mysql_query("SELECT name, id FROM users");

Then repeat the selection and then the while loop to iterate through the various parameters:

<?php
echo "<select name='user'>";
while ($temp = mysql_fetch_assoc($query) {
    echo "<option value='".$temp['id']."'>".$temp['name']."</option>";
}
echo "</select>";
?>

The result should be:

<select name="user">
   <option value='1'>User name 1</option>
   <option value='2'>User name 2</option>
</select>

, :)

+3
+1

Source: https://habr.com/ru/post/1733561/


All Articles