-2

I am using a select statement to pull data from my DB using a where clause and limited to 1, then i am trying to put the addresses it has pulled into the sendto field on phpmailer.

I have updated the question so the full code is here, i am still unable to pull up the echo from the var that i entered to collect the emails from the list.

<?php
$servername = "localhost";
$username = "####";
$password = "####";
$dbname = "####";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection
if (!$conn) {
  die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";



$sql = "SELECT email FROM emails WHERE status = 'new' LIMIT 1";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
  // output data of each row
  while($row = $result->fetch_assoc()) {
    echo "id: " . $row["email"]. " ";
  }
} else {
  echo "0 results";
}


$sql = "UPDATE emails SET status='done' WHERE status = 'new' LIMIT 1";

if ($conn->query($sql) === TRUE) {
  echo "Record updated successfully";
} else {
  echo "Error updating record: " . $conn->error;
}


 $var_email = $row["email"]; 


use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

//Load Composer's autoloader
require 'vendor/autoload.php';
    Class SendMail{
        function send(){
            
            $mail = new PHPMailer(true);
            //Server settings
            $mail->SMTPDebug = SMTP::DEBUG_SERVER;;                  //Enable verbose debug output
            $mail->isSMTP();                                            //Send using SMTP
            $mail->Host       = '####';                     //Set the SMTP server to send through
            $mail->SMTPAuth   = true;                                   //Enable SMTP authentication
            $mail->Username   = '####';                     //SMTP username
            $mail->Password   = '####';   
               $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;            //Enable implicit TLS encryption
    $mail->Port       = 465;   
    
            //Recipients
          $mail->setFrom('info@####', '####');
         $mail->addAddress($var_email);     
               

    
            //Content
            $mail->isHTML(true);                                  //Set email format to HTML
            $mail->Subject = '####';
            $mail->Body    =   "####";
    
            $mail->send();
            
          
            echo 'Message has been sent';
        }
        
    }
    
    
        
        $sendMail = new SendMail();
        $sendMail->send();
        
$conn->close();



   
?>

I am hoping its a simple fix, but i feel like somewhere i have put the wrong string which is not allowing me to echo the results into the address field.

any suggestions?

jim
  • 1
  • 2
  • To get errors out of PHP _even in a LIVE environment_ add these 4 lines **temporarily, while debugging**, to the top of any `MYSQLI_` based script you want to debug `ini_set('display_errors', 1); ini_set('log_errors',1); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);`. This will force any `MYSQLI_` errors to generate an Exception that you can see on the browser as well as normal PHP errors. – RiggsFolly Sep 21 '22 at 10:16
  • ___NOTE___ If you add a LIMIT 1 to the query, then you will get Zero or One row in the resultset, so doing a While loop to fetch the single result is not necessary and confusing to the dev that follows you to maintain this code – RiggsFolly Sep 21 '22 at 10:18
  • So where does this line of code `$mail->addAddress($row["email"])` exist? Is it in the same file as the code above it? – RiggsFolly Sep 21 '22 at 10:21
  • it is in the same file but below the SELECT result in a phpmailer code – jim Sep 21 '22 at 10:29
  • you did a `echo "id: " . $row["email"]. " ";` did that show anything? – RiggsFolly Sep 21 '22 at 10:31
  • it wont load the page when i try that, i have updated my full source code – jim Sep 21 '22 at 12:28

1 Answers1

-4

take a variable and assign value to it

ex. $var_email = $row["email"]; $mail->addAddress($var_email);

try it it's working. problem is occur because of php version