How to put information from a bank in a select field in PHP

0

I have a page that connects to the database and brings information correctly, however I would like to put information on a form using the select field, but I'm having some difficulties if someone can help me follow my code.

<?php
require_once 'init.php';

// abre a conexão
$PDO = db_connect();

// SQL para selecionar os registros
$sql = "SELECT cliente FROM clientes";

// seleciona os registros
$stmt = $PDO->prepare($sql);
$stmt->execute();

?>

<html>
<head>
    <meta charset="utf-8">
    <title>Lab</title>
</head>
<body>
    <h1>Lab</h1>

    <?php while ($user = $stmt->fetch(PDO::FETCH_ASSOC)): ?>
    <?php echo $user['cliente'] ?>
    <?php endwhile; ?>

</body>
</html>
    
asked by anonymous 17.03.2016 / 22:19

2 answers

4

To make a select with your data:

<select>
  <?php while ($user = $stmt->fetch(PDO::FETCH_ASSOC)) { ?>
      <option value="<?php echo $user['cliente'] ?>">
        <?php echo $user['cliente'] ?>
      </option>
  <?php }  ?>
</select>
    
17.03.2016 / 22:32
-2
<select>
<?php 
    while ($user = $stmt->fetch(PDO::FETCH_ASSOC)){
        echo '<option value="'.$user['cliente'].'">'.$user['cliente'].'</option>';
    }
?>
</select>
    
18.03.2016 / 02:26