title: How to send an email to an address from my database?
body: <p>I have a form that sends email and this all works fine but now I want to send the email to an address that is stored in a mysql database but I can't figure out how to do this. No matter what I try to make '$to' in the mail script below semd to the variable pulled from the database it does not work.
  Can anyone tell what am I doing wrong? Thanks.</p>
  
  <pre><code>/// Get get recipients email address from database using id
  $id= $_GET["id"];
  $result = mysqli_query($conx, "SELECT email from mytable where id='$id'");
  while($row = mysqli_fetch_array($result)) {
  $to = $row["email"];
  }
  
      if ($_GET["submit"]) {  
          $name =  htmlspecialchars($_GET['name']);
          $email = htmlspecialchars($_GET['email']);
          $message = $_GET['message'];
          $subject = 'Hello There';
          $body = "E-Mail: $email\n Message: $message";
          if (mail($to, $subject, $body)) {
  
          $result='&lt;div class="alert alert-success"&gt;Your email has been sent&lt;/div&gt;';
               }else{
          $result='&lt;div class="alert alert-danger"&gt;Sorry there was an error sending your message.&lt;/div&gt;';
      }
  }
  ?&gt;
  </code></pre>