title: How to send email to address fetched from database via php mailer
body: <p>I have made page where the user enters the data &amp; send email.First i want to insert data into db &amp; then trigger mail to registered email id.Till now i was manually typing the email id. What should be done to fetch the email id from database &amp; send it. </p>
  
  <pre><code>    &lt;?php 
      $host="localhost"; // Host name 
      $username="root"; // Mysql username 
      $password=""; // Mysql password 
      $db_name="testmra"; // Database name 
  
      // Connect to server and select databse.
      $conn=mysqli_connect($host,$username,$password) or die("cannot connect"); 
  
      mysqli_select_db($conn,$db_name);
      $sname=$_SESSION['usr_name'];
  
  
      $room = mysqli_real_escape_string($conn, $_POST['txtrname']);
      $name = mysqli_real_escape_string($conn, $_POST['txtname']);
      $dept = mysqli_real_escape_string($conn, $_POST['txtdept']);
      $purpose = mysqli_real_escape_string($conn, $_POST['txtpurpose']);
      $attendee = mysqli_real_escape_string($conn, $_POST['attendee']);
      $date = mysqli_real_escape_string($conn, $_POST['txtdate']);
      $btime = mysqli_real_escape_string($conn, $_POST['btime']);
      $etime = mysqli_real_escape_string($conn, $_POST['etime']);
  
      $sql="INSERT INTO bookingdetails (room,name,department,purpose,attendee,date,starttime,endtime,status_id)VALUES('$room','$name','$dept','$purpose','$attendee','$date','$btime','$etime','2')";
  
      $res  = mysqli_query($conn,"SELECT emailid FROM newuser WHERE username='$sname'");
  
      $row = mysqli_fetch_assoc($res);
      $to = $row["emailid"];
  
      require('phpmailer/PHPMailerAutoload.php');
  
      $mail = new PHPMailer();
  
      $subject = "Bookig Details";
      $content = "&lt;b&gt;Hello $name. Your Booking Details are as follow. Room : $room Date : $date Start Time : $btime End Time : $etime&lt;/b&gt;";
      $mail-&gt;IsSMTP();
      $mail-&gt;SMTPDebug = 0;   
      $mail-&gt;SMTPAuth = TRUE; 
      $mail-&gt;SMTPSecure = "no";
      $mail-&gt;Port     = 26;  
      $mail-&gt;Username = "admin";
      $mail-&gt;Password = "@@@";
      $mail-&gt;Host     = "59.68.1.101";
      $mail-&gt;Mailer   = "smtp";
      $mail-&gt;SetFrom("admin@hitechplast.in", "Admin");
      $mail-&gt;AddReplyTo("admin@hitechplast.in", "Admin");
      $mail-&gt;AddAddress($to);
      $mail-&gt;Subject = $subject;
      $mail-&gt;WordWrap   = 80;
      $mail-&gt;MsgHTML($content);
      $mail-&gt;IsHTML(true);
  
      if(!$mail-&gt;Send())
  {
     echo "Message could not be sent. &lt;p&gt;";
     echo "Mailer Error: " . $mail-&gt;ErrorInfo;
     exit;
  }
  
  echo "Message has been sent";
  
      if (mysqli_query($conn,$sql))
        {
          echo "Record added";
  
          }
      else
      {
          die('Error: ' . mysqli_error());
       }
  ?&gt;
  </code></pre>