Forum Moderators: coopster

Message Too Old, No Replies

Script Has Unexpected Loop in MySQL Query

         

itledi

2:22 pm on Dec 17, 2007 (gmt 0)

10+ Year Member



This has to be one of my stupidist coding moments.

I wrote this script to check my MySQL table, and to send an email to all enteries where the date was equal to today's date and where delivered="no". After all the emails were sent, then the script was to set delivered="yes" for all enteries in my table where the date was equal to today's date.

What I got was a vicious loop cycle where it just sent out thousands of emails to my email account.

Please, show me where my bug is:

while($email=mysql_fetch_array(mysql_query("SELECT * FROM table WHERE date=CURDATE() AND delivered='No'"))){
mail($email["to"],"Subject","Body",$from_headers);
}

mysql_query("UPDATE table SET delivered='Yes' WHERE date=CURDATE()");

cameraman

5:34 pm on Dec 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can't do that all in one line, because each time through it's going to do a new query and get the first matching record in the table.
$qry = mysql_query($sql);
while($email = mysql_fetch_array($qry) {
mail();
}
mysql_query("UPDATE");

itledi

1:57 pm on Dec 18, 2007 (gmt 0)

10+ Year Member



Thank you so much! Taking out that variable completly fixed the problem.

Thanks again, that was a nasty bug that even though it ran only for a couple of seconds till I aborted, it sent me several thousands of emails over the next couple of hours.