Jump to content

Limit per hour without repeat


SkyRanger

Recommended Posts

I was wondering if there way way to limit the amount of emails per hour and not repeat the same emails twice, with the code I have?

 

$query = "SELECT email FROM users";
$result = mysql_query($query) or DIE (mysql_error());

while ($r = mysql_fetch_array($result)) { $emailAry[] = $r['email']; }

if (count($emailAry)) {
       $list = implode(", ", $emailAry);
       $to = "[email protected]"; 
       $subject = "Monthly Newsletter";
       $body= $postnewsletter;
       $headers .= "Bcc: $list\r\n";
       mail($to, $subject, $body, $headers);
} else {
       echo "no users";
}

Link to comment
https://forums.phpfreaks.com/topic/273169-limit-per-hour-without-repeat/
Share on other sites

ok was just testing to code, and I screwed something up. This isn't right because it does not update database. How could I do this to update the database once email has been sent?

 

//send Newsletter to subscribers based on type (text or html)
$query = "SELECT email FROM newsletter WHERE nltype='1' and lastEmailed < (NOW() - INTERVAL 1 HOUR) LIMIT 0,50 ";
$postnewsletter = "This is a newsletter test"; //will be pulled from database
$result = mysql_query($query) or DIE (mysql_error());

while ($r = mysql_fetch_array($result)) { $emailAry[] = $r['email']; }

if (count($emailAry)) {
               $list = implode(", ", $emailAry);
               $to = "[email protected]"; 
               $subject = "Insurance eSafe Newsletter";
               $body= $postnewsletter;
               $headers .= "Bcc: $list\r\n";
               mail($to, $subject, $body, $headers);
$query1 = ("update newsletter set lastEmailed = date('Y-m-d H:i:s') where email='$list'") or mysql_error();                
} else {
               echo "no users";
}

First, your $list variable is just that, a list. You won't have any matches for "WHERE email=$list" in your database. So you'll either want to move the query into the while loop or put the query in a for loop.

 

Second, just in case the code you posted is the code you're working with, you're not actually running the query to update the lastEmailed. You're only creating a string and storing it to your $query1 variable.

 

Lastly, since you're writing new code, I strongly suggest you start using mysqli_* as mysql_* is deprecated. See the large red warning on every mysql_* manual page? Example.

Ok, thanks charlie, For the first, yeah I will have to move that so It will update on each email submission and update. Second, thanks for catching that., and lastly, just checked out the mysqli_ and mysql_ and noticed, got some reading to do before I continue with this script. Thank you. Will post when completed new code.

Cool cool. Happy to help. I'll look out for more code.

 

Yeah, I made the switch from mysql_* to mysqli_* myself fairly recently. It's actually not too bad.

 

Here's a small bit example code that might help you get started with how to make the switch:

 

<?php
function db_connect()
{
$hostname = 'localhost';
$username = 'root';
$password = 'root';
$database = 'my_db_table';

$link = mysqli_connect($hostname, $username, $password, $database)
	or die('Connect Error ('.mysqli_connect_errno().')');

return $link;
}

$link = db_connect();
$query = "SELECT * FROM `table` ORDER BY `title` ASC";
if ( $result = mysqli_query($link, $query, MYSQLI_STORE_RESULT) )
{
echo '<table>';
echo '<tbody>';
while ( $r = mysqli_fetch_array($result, MYSQLI_ASSOC) )
{
	$var = $r['field'];
	echo '<tr>';
	echo '<td>'.$var.'</td>';
	echo '</tr>';
}
echo '</tbody>';
echo '</table>';
}
else
{
mysqli_close($link);
die('<h1>Unable to process query: make_table</h1>');
}
mysqli_close($link);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.