fetch email in the bank and make appear in a select with php

-1

(this select is an example) Well I have one here a select that will serve to send an email with phpMailer. I want to know how I can do so that in this select only the emails registered in my database "banked" within the "teacher" table appear. Email is a teacher attribute. If you need more information just say so.

<div class="input-field col s12">
    <select>
      <option value="" selected>Choose your option</option>
      <option value="1">Option 1</option>
      <option value="2">Option 2</option>
      <option value="3">Option 3</option>
    </select>
    <label>email</label>
  </div>
    
asked by anonymous 27.06.2017 / 00:59

3 answers

0

With mysqli

$con = new mysqli ("localhost", "USUARIO", "SENHA", "professor");
if($con->connect_errno){
    echo "falha na conexão";
    exit();
}
$consulta = "SELECT email,nome FROM professor";

$result = $con->query($consulta);

echo '<form action="Pagina_Destino" method="post"></form>

<div class="input-field col s12">
    <select>
      <option value="" selected>Choose your option</option>';

while($dados = $result->fetch_array()){
     $email = $dados["email"];
     $nome = $dados["nome"];
     echo "<option value=\"".$email."\">".$nome."</option>";
} 

    echo '</select>
    <label>email</label>
  </div>
  </form>';
    
27.06.2017 / 02:06
0

1) Use the PDO.

2) Connect to the database using the PDO.

$db_username        = 'usuario';
$db_password        = 'senha';
$db_name            = 'nomedobancodedados';
$db_host            = '127.0.0.1';

try {
    $mysqli_conn = new PDO("mysql:host=$db_host;dbname=$db_name", $db_username, $db_password);
} catch (PDOException $e) {
    print "Erro: " . $e->getMessage() . "<br>";
    die();
}

3) Determine a variable that performs the SQL query.

$results = "
SELECT colunadesejada AS 'emails'
FROM tableadesejada
WHERE filtrosdesejados
ORDER BY ordemdesejada
"

4) Use to execute.

$stmt = $mysqli_conn->prepare($results);
$stmt->execute();

5) Use to display the results.

while($row = $stmt->fetch()) {
echo '<div class="aaa">'.$row["emails"].'</div><br>'; //htmlquevocequiser
}

Your question does not show any PHP attempts, this is a pity.

    
27.06.2017 / 01:32
0

You can do this simply by using mysqli_fetch_row() of mysqli .

<?php
$connect = mysqli_connect('localhost','bancodados','senha', 'root'); //Infos do BD
$comando = "SELECT email FROM professor"; //Comando SQL
?>
<div class="input-field col s12">
    <select>
        <option value="" selected>Choose your option</option>
        <?php
        if ($result=mysqli_query($connect,$comando)) //Checa se deu algum erro
        {
            $i = 0;
            while ($row=mysqli_fetch_row($result)) //Pega o resultado do comando para usa-lo
            {
                $i++;
                echo"<option value="$i">$row[0]</option>";//Adiciona o option
            }
        }
        ?>
    </select>
    <label>email</label>
  </div>
    
27.06.2017 / 02:09