Reputation: 11
Is it possible to select a new row from mysql database whenever I call a php script? For example, I need to fetch a new email address from a mysql database everytime I call the following php script. I am aware of mysqls' ORDER BY RAND ()
function; here I need to fetch every row according to their date of entry (i.e. ORDER BY created_on DESC
).
// Add recepients from a datasoutce iteratively
$emails = mysql_query('SELECT email from recepients ORDER BY created_on DESC LIMIT 1') or die;
while($row = mysql_fetch_assoc($emails))
{
message->addTo($row['email']);
}
EDIT: I will use cron job (scheduled task in windows) to run my php script, where I need a solution to select rows one by one (not randomly) as my php is called.
Upvotes: 1
Views: 48
Reputation: 5868
Add column to table, if you are allowed to:
alter table recepients add column uts_sent BIGINT NOT NULL;
Update the table after sending the email:
$limit= 1; //fetch $limit rows per page
$now = mktime(); //current unix time stamp
// Add recepients from a datasource iteratively
$emails = mysql_query("SELECT email from recepients WHERE uts_sent<$now ORDER BY created_on DESC LIMIT $limit") or die;
while($row = mysql_fetch_assoc($emails))
{
$message->addTo($row['email']);
$message->send();
$email_escaped = mysql_real_escape_string($row['email']);
mysql_query("update recepients set uts_sent = $now where email ='$email_escaped'") or die;
}
Upvotes: 1