Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SELECT and mail() function query 1

Status
Not open for further replies.

brownfox

Programmer
Joined
Jan 5, 2003
Messages
173
Location
GB
I want to query my database with a SELECT statement and send email to the results:

$username="********";
$password="********";
$database="********";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT email2,handle,passwrd FROM profiles WHERE status = 'old'";
$result=mysql_query($query) or die (mysql_error());
$num=mysql_numrows($result);
$i=0;
while ($i<$num){
$handle=mysql_result($result,$i,&quot;handle&quot;);
$email2=mysql_result($result,$i,&quot;email2&quot;);
$passwrd=mysql_result($result,$i,&quot;passwrd&quot;);

//add From: header
$headers = &quot;From: webmaster@$SERVER_NAME\r\n&quot;;

//specify MIME version 1.0
$headers .= &quot;MIME-Version: 1.0\r\n&quot;;

//unique boundary
$boundary = uniqid(&quot;HTMLDEMO&quot;);

//tell e-mail client this e-mail contains//alternate versions
$headers .= &quot;Content-Type: multipart/alternative&quot; .
&quot;; boundary = $boundary\r\n\r\n&quot;;

//message to people with clients who don't
//understand MIME
$headers .= &quot;Important notice from webmaster\r\n\r\n&quot;;

//plain text version of message
$headers .= &quot;--$boundary\r\n&quot; .
&quot;Content-Type: text/plain; charset=ISO-8859-1\r\n&quot; .
&quot;Content-Transfer-Encoding: base64\r\n\r\n&quot;;
$headers .= chunk_split(base64_encode(&quot;Dear $handle,\r\n Please login with these details username:$handle. Pasword:$passwrd.&quot;));

//HTML version of message

$headers .= &quot;--$boundary\r\n&quot; .
&quot;Content-Type: text/html; charset=ISO-8859-1\r\n&quot; .
&quot;Content-Transfer-Encoding: base64\r\n\r\n&quot;;
$headers .= chunk_split(base64_encode(&quot;Dear $handle,<p>
Please login with these details username:$handle. Pasword:$passwrd&quot;));

$recipient = &quot;$email2&quot;;
$subject = &quot;Important notice from webmaster&quot;;
$message = &quot;&quot;;
$extra = &quot;From: webmaster@$SERVER_NAME\nReply-To: webmaster@$SERVER_NAME&quot;;
mail ($recipient, $subject, $message, $headers) or die(&quot;No go&quot;);
++$i;
}

But this sends 8 emails to each account!Can anyone explain why this does so? I think it must be a problem with the loop or something. Thanks in advance.
 
It's returning an email address and a nickname and a password. Then the same triplet but for another record etc. about 150 times(no duplicates).So I guess the issue must be in the mail() bit?
 
It's hard to say. I rarely use mysql_result() to fetch data from a query return -- the online manual says that the row-at-a-time fetching functions are faster in an application where you are fetching more than one value from each row.

Something you might try:

Change:
Code:
$num=mysql_numrows($result);
$i=0;
while ($i<$num){
$handle=mysql_result($result,$i,&quot;handle&quot;);
$email2=mysql_result($result,$i,&quot;email2&quot;);
$passwrd=mysql_result($result,$i,&quot;passwrd&quot;);

to read:

Code:
while (list ($email2, $handle, $passwrd) = mysql_fetch_row($result))
{

This will eliminate any problems you might be having with return handle result indeces.

Want the best answers? Ask the best questions: TANSTAAFL!
 
Thanks that returns the same when I comment out the mail() action. I'll try it with the mail() function and see what happens! It's just that when I ran my first script it sent out 200 emails and 1400 duplicates (oops a bit embarrassing!). Thanks for your help, I've starred it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top