looping a query from multiple variables?
I'm rewriting my newsletter mailer to throttle sending to certain domains. basically subscribers are saved in mailer_lists
in another form, then they get assigned the mailing ip to use with that mailing ip's limit for the specified domain. the code below is my attempt to gather that information.
What I'm trying to do is pull records for query matches, looping through $node_ip
, $throttle_domain
, and $throttle_speed
and then stop pulling records if it hits the global limit, then send. i'm having trouble getting it to work right..
function queue(){
$query = "S开发者_JAVA百科ELECT * FROM `mailer_lists` WHERE `ip` = '$node_ip' AND `email` LIKE '%".$throttle_domain."' LIMIT ".$throttle_speed."" ;
$result = mysql_query($query) or die(mysql_error());
$num_rows = mysql_num_rows($result);
$count = $num_rows;
}
if ($count < $global){
queue();
}else{
mail();
Wish I had 1/2 the skills some of you have. Looking forward to any ideas..Make you function return number of rows like this:
function queue(){
$query = "SELECT * FROM `mailer_lists` WHERE `ip` = '$node_ip' AND `email` LIKE '%".$throttle_domain."' LIMIT ".$throttle_speed."" ;
$result = mysql_query($query) or die(mysql_error());
return mysql_num_rows($result);
}
No you can make your condition:
if (queue() < $global){
queue();
}else{
mail();
The count in your function isn´t marked as global so is not updated.. Add global $count as the first line of the queue function.
Another issue is that when the count never reaches the global var, it will never mail. So the last batch wont be sent.
I don't really if this will work at all :D
<?php
function queue($ptr, $glbl){
/* each time we increment our range by $glbl */
$newPtr = $ptr + 100;
/* we use limit keyword to only fetch 100 rows each time */
$query = "SELECT * FROM `mailer_lists` WHERE `ip` = '$node_ip' LIMIT '$ptr', '$newPtr'" ;
$result = mysql_query($query) or die(mysql_error());
/* fetch the rows and send to theme an email */
while($row = mysql_fetch_row($result))
{
mail(mail($row['email'], $subject, $message));
}
}
/* here we use a static variable to track where we are we in the range */
static $next = 0;
$i = 0;
/* sending emails*/
queue($next, $glbl);
/* put pur next beginning range in
$next ant it will end with $next + $glbl as it in line 5 */
$next += $glbl;
?>
精彩评论