LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-01-2005, 07:10 PM   #1
newuser455
Member
 
Registered: May 2004
Location: Texas
Posts: 277

Rep: Reputation: 30
Question PHP/MySQL table question


I am trying to write a script that has a mass mail function, which sends an email to everyone in a database.

My MySQL table looks like this:

id name email



Anyways, all I want to do is output the email column to a string, such as

PHP Code:
$emails email from row 1email from row 2email from row 3 
etc....


Does anyone know how I could do this, or a better way to set up the table?

Thanks in advance.
 
Old 11-02-2005, 03:20 AM   #2
fouldsy
Senior Member
 
Registered: Jan 2002
Location: St Louis, MO
Distribution: Ubuntu
Posts: 1,284

Rep: Reputation: 47
The easiest way is to run a loop whilst executing your query, and stick the e-mail addresses into an array. Something like:
Code:
$i=0;
$result = @mysql_query("SELECT email FROM tablename");
while ($row = mysql_fetch_array($result)) {
     $email[$i] = $row["email"];
     $i++;
}
You will then have all your e-mail address stored in an array $email. A simple loop can then be used again to e-mail out whatever you need.
 
Old 11-02-2005, 03:45 AM   #3
zeitounator
Member
 
Registered: Aug 2003
Location: Montpellier, France, Europe, World, Solar System
Distribution: Debian Sarge, Fedora core 5 (i386 and x86_64)
Posts: 262

Rep: Reputation: 30
Quote:
Originally posted by fouldsy
You will then have all your e-mail address stored in an array $email. A simple loop can then be used again to e-mail out whatever you need.
Supposing (from the different pieces of code I can read) you are using php as your scripting language, and supposing you really want to put all these emails in a variable separated by commas, you don't even need a loop. Just use the implode() fuction (alias join()).

Code:
$emailfield = implode(',', $email);
See the implode function doc: http://www.php.net/manual/en/function.implode.php
 
Old 11-02-2005, 04:12 AM   #4
fouldsy
Senior Member
 
Registered: Jan 2002
Location: St Louis, MO
Distribution: Ubuntu
Posts: 1,284

Rep: Reputation: 47
That would work, but I don't see why the e-mail address would be constructed into a string before e-mail out. You'd end up with a massive string. Depending on the exact number of addresses being used, it might hit the size limit for a string variable. Plus, what if you then wanted to filter them or sort them? An array is much easier to work with than a long string. Effectively, you'd be e-mailing to multiple people from the one mail, meaning everyone sees the other addresses. The alternative is to run through each address as separate mail. You still the send the same number of e-mails, but in a slightly cleaner way.
 
Old 11-02-2005, 05:01 AM   #5
zeitounator
Member
 
Registered: Aug 2003
Location: Montpellier, France, Europe, World, Solar System
Distribution: Debian Sarge, Fedora core 5 (i386 and x86_64)
Posts: 262

Rep: Reputation: 30
Quote:
Originally posted by fouldsy
That would work, but I don't see why the e-mail address would be constructed into a string before e-mail out. You'd end up with a massive string.
This is why I took the precaution to state: supposing you really want to put all these emails in a variable separated by commas
 
Old 11-02-2005, 06:58 AM   #6
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu
Posts: 1,415

Rep: Reputation: 99
maybe you are better off using a perl script for that job. if u have to send huge numbers of emails the php script may run out of time. There is a maximal execution time assigned in php.ini.
You better keep track of where you already sent a mail in the database in case the script does not run thru the whole array of addresses. If u use php this is very likely to happen. So run an "update"-statement while looping thru the array...
cheers, j.
 
Old 11-02-2005, 07:23 AM   #7
newuser455
Member
 
Registered: May 2004
Location: Texas
Posts: 277

Original Poster
Rep: Reputation: 30
Hmm.. well.. I came up with this code which actually works.

PHP Code:
//Retrieve email addresses
$emails '';
while ((
$row mysql_fetch_array($result)) !== false){
$emails .= $row['email'].', ';

$emails trim($emails', ');
$emails_array explode(', '$emails); 
 
Old 11-02-2005, 07:36 AM   #8
fouldsy
Senior Member
 
Registered: Jan 2002
Location: St Louis, MO
Distribution: Ubuntu
Posts: 1,284

Rep: Reputation: 47
Glad you got something going. Have fun!
 
Old 11-02-2005, 08:46 AM   #9
zeitounator
Member
 
Registered: Aug 2003
Location: Montpellier, France, Europe, World, Solar System
Distribution: Debian Sarge, Fedora core 5 (i386 and x86_64)
Posts: 262

Rep: Reputation: 30
Quote:
Originally posted by newuser455
Hmm.. well.. I came up with this code which actually works.

PHP Code:
//Retrieve email addresses
$emails '';
while ((
$row mysql_fetch_array($result)) !== false){
$emails .= $row['email'].', ';

$emails trim($emails', ');
$emails_array explode(', '$emails); 
Or the art to do it the complicated way when it can be simple.....

PHP Code:
// retreive all email addresses
while (($row mysql_fetch_array($result)) !== false){
      
$emails_array[] = $row['email'];

 
Old 11-02-2005, 03:31 PM   #10
newuser455
Member
 
Registered: May 2004
Location: Texas
Posts: 277

Original Poster
Rep: Reputation: 30
If I need it in a string, I can use $email, if I need an array I can use $email_array. Works fine for me .
 
Old 11-03-2005, 04:50 AM   #11
zeitounator
Member
 
Registered: Aug 2003
Location: Montpellier, France, Europe, World, Solar System
Distribution: Debian Sarge, Fedora core 5 (i386 and x86_64)
Posts: 262

Rep: Reputation: 30
Quote:
Originally posted by newuser455
If I need it in a string, I can use $email, if I need an array I can use $email_array. Works fine for me .
PHP Code:
// retreive all email addresses
while (($row mysql_fetch_array($result)) !== false){
      
$emails_array[] = $row['email'];
}

// Then use a loop to send email ...
foreach ($emails_array as $email) {
    
doSomeStuffWith($email);
}

// Or join all emails in a string
mail(implode(','$mail_array), 'A test message''The content of test message''From: [email]blabla@nowhere.com[/email]'); 
Doing it this way as several (non exhaustive list....) advantages over your original piece of code:
  • you don't make unnecessary string operations
  • you don't use memory for a second variable containing duplicate data that you might not use
  • you limit the chances to have an other piece of code modifying one variable and not the other
  • your keep the ability to sort or filter your array before you implode it
  • you keep your code more clean, readeable and understandable
  • etc.
But you can of course still use you're piece of code that "works"
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem inserting data into a mysql table using PHP Rockgod2099 Programming 13 08-03-2005 12:27 AM
PHP/MySQL Question Corey Edwards Linux - Software 4 01-14-2005 12:18 PM
php mysql question infected Programming 2 10-31-2004 09:50 PM
MySQL non-realtime table-by-table mirroring Passive Linux - Software 1 01-20-2004 12:11 PM
How to import MS ACCESS Table including OLE filed into the MySQL Table ? myunicom Linux - General 1 11-28-2003 11:30 AM


All times are GMT -5. The time now is 08:53 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration