Email all records in one email

how do i get all the records from the db and send it in an 1 email?

this is the code i have which sends 1 email for each record.


$sql="SELECT EMAIL, CC4Didgits, LPNUMBER FROM MUPASS WHERE email='".$email."'";
$r = mysql_query($sql);

if(mysql_affected_rows()==0){

    print "<font color='#FFFF00'><b>The email address you entered is not in our system.  Please use the email address that is registered with us.</b></font><br>";

   // exit();
}
else {

    $row=mysql_fetch_array($r); 
    
   
    $email=$row["EMAIL"];
	$credit=$row["CC4Didgits"];
	$licplate=$row["LPNUMBER"];

	$headers = "X-Mailer: New Member App PHP Script\\r\
";
 
    $headers .= "MIME-Version: 1.0\\r\
";
    $headers .= "Content-type: text/html; charset=iso-8859-1\\r\
";  


    $content="To login to your account, please use your email address and one of the below.<br>\
";
	$content .="CC ".$credit."<br>\
";
	$content .="License Plate ".$licplate;

    mail($email, $subject, $content, $headers);
	
    print "<font color='#FFFF00'><b>An email has been sent with your login information.  If you don't see it, please check your spam/junk folder.</b></font><br>";
	
   }

Are you saying you want to send every row from the DB to this one email address ?

You do not have to concatenate in the query.

$sql="SELECT EMAIL, CC4Didgits, LPNUMBER FROM MUPASS WHERE email = '$email'";

Unless you are using an older version of PHP you should upgrade to mysqli_ commands.

If you insist on using inline styles for your HTML you may want to use proper inline CSS.

This…

print "<font color='#FFFF00'><b>The email address you entered is not in our system. Please use the email address that is registered with us.</b></font><br>";

…should become:


print '<span style="color:#FFFF00; font-weight:bold;">The email address you entered is not in our system. Please use the email address that is registered with us.</span><br>';

You have what appear to be error messages being displayed as yellow (#FFFF00) text whereas traditionally they are shown as red (#FF0000). This is an easy mistake to make when typing out hex values manually (if you did not intend them to be yellow in the first place).

Not everyone likes the PHP MySQLi extension. I don’t. Plus it isn’t guaranteed to be available on every web host. Also, I happen to like concatenation over embedding variables into the string. Most syntax highlighting editors make such variables stick out more.

I just hope the $email variable is being sanitized prior to the SQL query’s construction or that database is just begging someone to perform a SQL injection on it:

Out of curiosity, what about the mysqli_ extension do you not like? To use a recent SitePoint blog there seems to be strong evidence to suggest mysqli_ is the way to go (again, if it’s available on your web host).

As for the concatenation, in the end it will not be a major issue in terms of performance. If you do it like the OP’s example for the sake of variable visibility in a particular IDE then to each their own.

syncup,i would like to send all records that match the email address.

Try this:


$sql = "select email, cc4didgits, lpnumber from mupass where email='" . mysql_real_escape_string($email) . "'";
$r = mysql_query($sql) or die(mysql_error());

if (mysql_affected_rows() == 0)
{
	print "<font color='#ffff00'><b>the email address you entered is not in our system. please use the email address that is registered with us.</b></font><br />";
}
else
{
	$headers = "X-Mailer: New Member App PHP Script\\r\
";

	// To send HTML mail, the Content-type header must be set
	$headers  = 'MIME-Version: 1.0' . "\\r\
";
	$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\\r\
";


	$headers .= "From: " . $email . "\\r\
";

	$subject = "Test subject\\r\
";


	$content = "To login to your account, please use your email address and one of the below.<br /><br />";
  
	while ($row = mysql_fetch_array($r))
	{
		$content .= "CC " . $row['cc4didgits'] . "<br />";
		
		$content .= "License plate " . $row['lpnumber'] . "<br />";
	}

	$sent = mail($email, $subject, $content, $headers);
  
	if ($sent)
	{
		print "<font color='#ffff00'><b>an email has been sent with your login information. if you don't see it, please check your spam/junk folder.</b></font><br>";
	}
}

ernie1,

tried your code and now it’s not even sending the emails anymore. doesn’t give any errors. when i submit the form it just alerts me that an email has been sent with my info, but nothing comes to my inbox. when i revert back to my old code it sends one of the records associated with the email address.

I tested the code, it works, it send in a single mail all records associated with the email address of the mysql table.

Try debugging the code, look what part of the code gives error.
Also, look in the server error log.

Give us your mysql table records, so we can see exactly what it contains.

Thanks for the help…got it to work.

Unless you have to use query buffering PDO is the way to go. Fair warning - the PHP dev’s are considering removing mysqli, mysql, mssql, and all the other legacy db access libraries from core and putting them out into PECL starting with PHP 5.4. This means on the servers running that version or newer there will be no guarantee the legacy code will work unless the appropriate pecl lib is pulled in. And getting query buffering to work correctly under PDO is being looked at I think.