Click to See Complete Forum and Search --> : Sending Mail to Mailing list
stephanief
May 7th, 2007, 06:55 AM
Hello everyone,
I m a little stuck with a problem here. How do I email a message to all the email addresses in my Mysql database? I believe I need to use arrays but I am not very familiar with them. Please help.:blush:
penagate
May 7th, 2007, 06:56 AM
Have you been able to retreive data from your database?
What have you got so far?
stephanief
May 8th, 2007, 04:33 AM
Yes I have managed to get info from the database and echo it. I've also managed to update it and delete it. I know how to send mail to an address inthe code but I don't know much about arrays amd iteration ..I know I should use one of or a combination of the two...but how.
CODE:
<?php
//SQLUtils contains all the details of the database connection
require_once('SQL_Utils1.php');
//connect to the database
$dbConn = getDatabaseConnection();
if($query = "SELECT * FROM Mail_List") {
//if query does run print message
echo "done ";
//otherwise if query does not run print message
}else{
echo 'Problem';
echo '<br>';
}
//this is a bit of borowed code which didn t work
while($row = mysql_fetch_array($query)){
foreach( $row AS $key => $val ){
$$key = stripslashes( $val );
}
}
$result = runQuery($query, $dbConn);
//end connection to db
disconnectFromDatabase($dbConn);
?>
:confused:
penagate
May 8th, 2007, 06:10 AM
This line:
if($query = "SELECT * FROM Mail_List")
does not execute anything. What that statement does is assign the query text to the $query variable, and then test whether it can be evaluated to true or not, which it can because anything that isn't false or null is true. So it will always return true.
What you need to do, presumably, is this:
$dbConn = getDatabaseConnection();
$query = "SELECT * FROM Mail_List";
if ($result = runQuery($query, $dbConn)) {
// it worked
}
else {
// it didn't
}
Assuming1 that $result is a MySQL resultset resource, you can then do this (in the "it worked" part):
while ($row = mysql_fetch_assoc($result)) {
// magic here
}
In each iteration, $row will point to an array containing the field values. Use mail() (php.net/function.mail) to send an email.
1. I can't help any more than that at this point, as I am not aware of your database schema or how the SQL_Utils1.php script works.
stephanief
May 11th, 2007, 07:45 AM
Solved the problem thank you just needed a little push.
Thanks LOTS :)
penagate
May 11th, 2007, 07:52 AM
You're very welcome and now that the problem is solved we would appreciate it if you could help us out by marking it as such using the Thread Tools menu above the first post. :)
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.