Question:
PHP/MySQL Question: Easy 10 points!?
I Love Ubuntu
2009-05-15 15:17:41 UTC
i have a subscribe to reminder button that adds an e-mail address to a one-column table. I have a cron job running every day that runs a script. I want this script to go through every e-mail address in this table and send a message (that i can construct, I know how to use mail();). The big thing i need help with is looping through the database. The rest I can do.

Thanks,
II
Five answers:
anonymous
2009-05-18 16:13:52 UTC
$query = mysql_query("SELECT emailaddress FROM tablename");

$num_query = mysql_num_rows($query);

while($emails = mysql_fetch_assoc($query)){

$MyMail=new MyMail($To="$info['keyname']", $Subject="Subject", $Msg=Message or body, $From="From email address", $ReplyTo="reply to email address.or use from email address");

}




/***********************

CLASS :: MYMail

************************/

class MyMail{

var $To;

var $ToSender;//If you want the email to go to the sender

var $Subject;

var $Msg;

var $Headers;

var $From;

var $ReplyTo;

/*

$headers = 'From: webmaster@example.com' . "\r\n" .

'Reply-To: webmaster@example.com' . "\r\n" .

'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);

***&&&&&*******

For HTML Mail

'MIME-Version: 1.0' . "\r\n";

$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

*/

function MyMail($To, $Subject, $Msg, $From, $ReplyTo){

$this->To=(empty($To)) ? "0" : $To;

$this->Subject=(empty($Subject)) ? "0" : $Subject;

$this->Msg=(empty($Msg)) ? "0" : $Msg;

$this->From=(empty($From)) ? "0" : $From;

$this->ReplyTo=(empty($ReplyTo)) ? "0" : $ReplyTo;

$this->Headers="MIME-Version: 1.0" . "\r\n" . "Content-type: text/html; charset=iso-8859-1" . "\r\n" . "From:" . $this->From . "\r\n" . "Reply-To:" . $this->ReplyTo . "\r\n" . "X-Mailer: PHP/" . phpversion();



//Use this array if you want to send to only one person

$SetMail=array(

'To'=> $this->To,

'Subject'=> $this->Subject,

'Msg'=> $this->Msg,

'Headers'=> $this->Headers,

'From'=> $this->From,

'ReplyTo'=> $this->ReplyTo

);

//Use this array if you want it to go to multiple people (the sender/CC:/Bcc:)

/*

$SetMail=array(

'To'=> $this->To . "," . $ToSender,

'Subject'=> $this->Subject,

'Msg'=> $this->Msg,

'Headers'=> $this->Headers,

'From'=> $this->From,

'ReplyTo'=> $this->ReplyTo

);

*/

if(in_array("0",$SetMail)){

echo "
Something wrong with the mail! Please make sure all fields are filled in!
";

return;

}

else{

if(!mail($SetMail['To'], $SetMail['Subject'], $SetMail['Msg'], $SetMail['Headers'])){

echo "";

}

}

}

}

?>
Michael G
2009-05-15 15:27:29 UTC



// Formulate Query

$query = sprintf("SELECT email FROM table");



// Perform Query

$result = mysql_query($query);



// Check result

// This shows the actual query sent to MySQL, and the error. Useful for debugging.

if (!$result) {

$message = 'Invalid query: ' . mysql_error() . "\n";

$message .= 'Whole query: ' . $query;

die($message);

}



// Use result

// Attempting to print $result won't allow access to information in the resource

// One of the mysql result functions must be used

// See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc.

while ($row = mysql_fetch_assoc($result)) {

$emailAddr = $row['email'];

echo $emailAddr;



//SEND EMAIL HERE TO $EMAILADDR ($emailAddr)

}



// Free the resources associated with the result set

// This is done automatically at the end of the script

mysql_free_result($result);

?>
anonymous
2016-10-25 17:01:55 UTC
so that you're "undemanding 10 factors"... isn't really undemanding. yet when you're searching for an undemanding thanks to get some HTML code, that is this little bit of information: maximum Login/Logout circumstances run on a server. definite, yet another server that you ought to pay extra to apply. because of this I sugest employing a 2d or third party information superhighway website. once you've a gaming information superhighway website, attempt to get steam to allow you take advantage of their community. free. similar with blogs. you should use Twitters login/logout. in the different case, you would possibly want to choose more advantageous than code. you would possibly want to choose an exterior information superhighway host. wish I helped!
Christopher
2009-05-15 15:30:14 UTC
I'm not familar with the mail() function really, however you'd accomplish the 'looping' by doing something akin to this (from code I have made):



$query = "SELECT tutid,authid,catid,title,tutdesc, hits FROM tutorials WHERE catid=$getcatid ORDER BY tutid DESC";

$result = mysql_query($query) or die('Error : ' . mysql_error());



while($row = mysql_fetch_array($result, MYSQL_NUM))

{

list($tutid,$authid,$catid,$title,$desc,$hits) = $row;

}



Although you'd replace the query with your query (perhaps SELECT * FROM subscribers) and replace the 'list' line with you mail function.
K.C. Shekhar
2014-03-06 07:35:58 UTC
hey dear freinds i want to know discription of $num_query = mysql_num_rows($query); syntax please help me.....


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...