search email in the database and make it appear in a select with php

Asked

Viewed 498 times

-2

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

<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>
  • 1

    Favour edit your question, because in the way you are you can’t understand anything. Use of that link to base.

  • It’s better this way?

  • And the teacher table has what fields? idProfessor, nomProfessor and so on? Will pass this information from PHP to HTML via AJAX or has PHP along with HTML

  • fields name, sex, campus, email, password, training. PHP usage along with HTML.

  • which column name contains the emails?

  • Table teacher column email.

  • see the answer if it is easier to understand and take the test

  • I made a correction in the answer, I think it was very easy for you to understand

  • I am not saying that it should be my answer to accept, but read this post https://pt.meta.stackoverflow.com/questions/1078/como-e-por-que-aceitar-uma-resposta/1079#1079

Show 4 more comments

3 answers

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 results.

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

Your question shows no PHP attempt, that’s a shame.

  • It would not be instead of putting DIV, but rather putting OPTION, as the question ?

  • That is why there is a comment on the side of the line "htmlquevocequiser". I’m here to help him build the code, not deliver it ready for him.

  • I don’t know almost anything about php, it’s a school project. This PDO codex does not work, I think it has to do with the fact that the rest is not in PDO, it would not be possible to do otherwise?

  • You are using MSQLI or PDO in your project?

  • MYSQLI. I tried to start right on this PDO, but I couldn’t understand it. I stayed on MYSQLI

  • PDO is more secure and supports various types of databases, so I gave the answer using it. Try again with PDO and tell me what error you are getting, so I help you solve it here.

  • This is an isolated code, works autonomously.

Show 2 more comments

0


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>';
  • Look is giving error on this line: $result = $con->query($query);

  • This line is not PDO?

  • boy I put what I was testing here on my bench including my credentials, It

  • is not PDO is Mysqli

  • @Jadsonalves It is object oriented Mysqli, if I am not mistaken. What error is giving?

  • @Jadsonalves I edited and forgot to change the name of the bank etc... so it must have been a mistake

  • It worked, thank you very much. You always answer my questions and it always works. Thank you very much,

Show 2 more comments

0

You can do this simply using the 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>

Browser other questions tagged

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