hashstar Posted May 12, 2012 Share Posted May 12, 2012 Hi there, I am trying to use some php code to get a users email address from mysql database, can someone please check my code and see where i am going wrong? <?php $con = mysql_connect("*","*","*"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("ogs_mailinglist1", $con); $result = mysql_query("SELECT * FROM mailinglist"); while($row = mysql_fetch_array($result)) { echo $row['email'] . " " ; echo "<br />"; } // Grab our config settings require_once($_SERVER['DOCUMENT_ROOT'].'/config.php'); // Grab the FreakMailer class require_once($_SERVER['DOCUMENT_ROOT'].'/include/MailClass.inc'); // instantiate the class $mailer = new FreakMailer(); // Set the subject $mailer->Subject = 'This is a test'; // Body $mailer->Body = 'This is a test of my mail system!'; // Get the user's Email $sql = mysql_query("SELECT email FROM users WHERE 1"); while($row = mysql_fetch_object($result)) mysql_close($con); if(!$mailer->Send()) { echo 'There was a problem sending this mail!'; } else { echo 'Mail sent!'; } $mailer->ClearAddresses(); $mailer->ClearAttachments(); ?> I think the problem is with this part: // Get the user's Email $sql = mysql_query("SELECT email FROM users WHERE 1"); while($row = mysql_fetch_object($result)) but i don't know how to correct it Thanks, Hash Quote Link to comment https://forums.phpfreaks.com/topic/262435-how-to-get-users-email-address-from-mysql-database/ Share on other sites More sharing options...
trq Posted May 12, 2012 Share Posted May 12, 2012 You can remove the WHERE clause, it does nothing. Also, how do you know your not getting any result? You don't actually do anything with it. Quote Link to comment https://forums.phpfreaks.com/topic/262435-how-to-get-users-email-address-from-mysql-database/#findComment-1344925 Share on other sites More sharing options...
trq Posted May 12, 2012 Share Posted May 12, 2012 oh, and ps. Stop posting your database credentials on a public forum unless you want bad stuff to happen to your data. Quote Link to comment https://forums.phpfreaks.com/topic/262435-how-to-get-users-email-address-from-mysql-database/#findComment-1344926 Share on other sites More sharing options...
hashstar Posted May 12, 2012 Author Share Posted May 12, 2012 You can remove the WHERE clause, it does nothing. Also, how do you know your not getting any result? You don't actually do anything with it. Oh ok, i don't know if i am getting a result i thought that this part would send the email if(!$mailer->Send()) but i am not getting an email so assumed that it was a problem with getting the email from the database... p.s sorry about posting database info, will be more careful in future Quote Link to comment https://forums.phpfreaks.com/topic/262435-how-to-get-users-email-address-from-mysql-database/#findComment-1344930 Share on other sites More sharing options...
NLT Posted May 12, 2012 Share Posted May 12, 2012 Why don't you try echo the query to see if you're getting a result? Quote Link to comment https://forums.phpfreaks.com/topic/262435-how-to-get-users-email-address-from-mysql-database/#findComment-1344931 Share on other sites More sharing options...
hashstar Posted May 12, 2012 Author Share Posted May 12, 2012 Hi NLT, i added: echo $row['email'] and it prints the email on the screen so i know i am deftly getting the data from the database. But it won't send the email... Quote Link to comment https://forums.phpfreaks.com/topic/262435-how-to-get-users-email-address-from-mysql-database/#findComment-1344933 Share on other sites More sharing options...
NLT Posted May 12, 2012 Share Posted May 12, 2012 Are you using this on a server with SMTP? Quote Link to comment https://forums.phpfreaks.com/topic/262435-how-to-get-users-email-address-from-mysql-database/#findComment-1344934 Share on other sites More sharing options...
trq Posted May 12, 2012 Share Posted May 12, 2012 So, somehow you now need to pass the email address into the $mailer object. We would have no idea how to do that within seeing the class. Quote Link to comment https://forums.phpfreaks.com/topic/262435-how-to-get-users-email-address-from-mysql-database/#findComment-1344935 Share on other sites More sharing options...
hashstar Posted May 12, 2012 Author Share Posted May 12, 2012 NLT: yep i am using this on a server with SMTP Thorpe: Ah ok, this is the code from my MailClass file i am using if it's any help? <?php require_once($_SERVER['DOCUMENT_ROOT'].'/include/phpmailer.class.php'); class FreakMailer extends PHPMailer { var $priority = 3; var $to_name; var $to_email; var $From = null; var $FromName = null; var $Sender = null; function FreakMailer() { global $site; // Comes from config.php $site array if($site['smtp_mode'] == 'enabled') { $this->Host = $site['smtp_host']; $this->Port = $site['smtp_port']; if($site['smtp_username'] != '') { $this->SMTPAuth = true; $this->Username = $site['smtp_username']; $this->Password = $site['smtp_password']; } $this->Mailer = "smtp"; } if(!$this->From) { $this->From = $site['from_email']; } if(!$this->FromName) { $this-> FromName = $site['from_name']; } if(!$this->Sender) { $this->Sender = $site['from_email']; } $this->Priority = $this->priority; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/262435-how-to-get-users-email-address-from-mysql-database/#findComment-1345039 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.