简体   繁体   中英

Sending Custom Email to All Users - PHP Code Issue

I'm currently prototyping a concept, so am using PHPMailer to generate/send emails out. I will move to something like SendGrid as somewhat soon, but that is not the issue at the moment. I need to send an email to every user in the DB, but the email will be customized based on elements stored in the DB (specific to the individual user).

I have PHPMailer working to pull fields from the DB and sending the mail. However, when I try to add the ability to cycle through all users and generate an email to each one, I seem to be having some PHP/SQL issues.

The "users" table has the fields: cid, email, fname, lname. I need to pull the cid to pull other information in the DB and I need the email so I know where to send the message. Here is my SQL/PHP:

require_once('/PHPMailer_v51/class.phpmailer.php');
include $_SERVER['DOCUMENT_ROOT'] . '/NG/includes/db.inc.php';

try
{
$sql = 'SELECT * FROM users';
$result = $pdo->prepare($sql);
$result->execute();
}

catch (PDOException $e)
{
$error = 'Error fetching user information.';
include 'error.html.php';
exit();
}

 foreach ($result as $row);
 {
  try
  {
    $high_result = $pdo->query('SELECT title FROM book WHERE topic = "high" and cid = $row['cid']');
  }
  catch (PDOException $e)
  {
  $error = 'Error fetching fields from database!';
  include 'error.html.php';
  exit();
  }
  .... the rest of the for statement....

I'm getting an unexpected T_STRING error on the $high_result line. The "users" DB has the fields: cid, email, fname, lname. Am I not able to reference an array in the sql statement? If not, any ideas on how I should be trying to do this?

Thanks,

Twine

You have a ";" at your foreach line, remove that and it should work

the query should be

$high_result = $pdo->query('SELECT title FROM book WHERE topic = "high" and cid = $row[cid]');

you have included ' inside $row['cid'] , that was the mistake. and ; in foreach loop also a mistake remove that.

foreach ($result as $row) // The semicoln here is not required
 //... The rest of the statements

$high_result = $pdo->query("SELECT title FROM book WHERE topic = "high" and cid = '. $pdo->quote($row[cid]));

The quote, in case your field is a string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM