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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
11-01-2005, 07:10 PM
|
#1
|
|
Member
Registered: May 2004
Location: Texas
Posts: 277
Rep:
|
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 1, email from row 2, email from row 3
etc....
Does anyone know how I could do this, or a better way to set up the table?
Thanks in advance.
|
|
|
|
11-02-2005, 03:20 AM
|
#2
|
|
Senior Member
Registered: Jan 2002
Location: St Louis, MO
Distribution: Ubuntu
Posts: 1,284
Rep:
|
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.
|
|
|
|
11-02-2005, 03:45 AM
|
#3
|
|
Member
Registered: Aug 2003
Location: Montpellier, France, Europe, World, Solar System
Distribution: Debian Sarge, Fedora core 5 (i386 and x86_64)
Posts: 262
Rep:
|
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
|
|
|
|
11-02-2005, 04:12 AM
|
#4
|
|
Senior Member
Registered: Jan 2002
Location: St Louis, MO
Distribution: Ubuntu
Posts: 1,284
Rep:
|
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.
|
|
|
|
11-02-2005, 05:01 AM
|
#5
|
|
Member
Registered: Aug 2003
Location: Montpellier, France, Europe, World, Solar System
Distribution: Debian Sarge, Fedora core 5 (i386 and x86_64)
Posts: 262
Rep:
|
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 
|
|
|
|
11-02-2005, 06:58 AM
|
#6
|
|
Senior Member
Registered: Jan 2002
Location: germany
Distribution: ubuntu
Posts: 1,276
Rep:
|
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.
|
|
|
|
11-02-2005, 07:23 AM
|
#7
|
|
Member
Registered: May 2004
Location: Texas
Posts: 277
Original Poster
Rep:
|
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);
|
|
|
|
11-02-2005, 07:36 AM
|
#8
|
|
Senior Member
Registered: Jan 2002
Location: St Louis, MO
Distribution: Ubuntu
Posts: 1,284
Rep:
|
Glad you got something going. Have fun!
|
|
|
|
11-02-2005, 08:46 AM
|
#9
|
|
Member
Registered: Aug 2003
Location: Montpellier, France, Europe, World, Solar System
Distribution: Debian Sarge, Fedora core 5 (i386 and x86_64)
Posts: 262
Rep:
|
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'];
}
|
|
|
|
11-02-2005, 03:31 PM
|
#10
|
|
Member
Registered: May 2004
Location: Texas
Posts: 277
Original Poster
Rep:
|
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  .
|
|
|
|
11-03-2005, 04:50 AM
|
#11
|
|
Member
Registered: Aug 2003
Location: Montpellier, France, Europe, World, Solar System
Distribution: Debian Sarge, Fedora core 5 (i386 and x86_64)
Posts: 262
Rep:
|
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" 
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 07:57 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|