How do I add a list of emails to my database?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
amaximo
Forum Newbie
Posts: 1
Joined: Wed Oct 29, 2003 9:04 am

How do I add a list of emails to my database?

Post by amaximo »

I am new to this php/sql stuff and could use a hand.

I have a list of emails that I need to enter into my data base.

-can a script read a text file line by line and add the entries to a MySQL table?

-----------
-this works to insert one row at a time when I change the values by hand but thats to slow


INSERT INTO `mail` ( `id` , `fname` , `lname` , `email` )
VALUES (
'', Brian , Smith , 'Brian@mail.com'
);

------------
I want to read the list of emails and names and put them into my database with a script. Can it be done? and how?


------------
this is a sample of what my text file list looks like :

Brian Smith Brian@mail.com
Kelly Green Kelly@mail.com
Bob Brown Bob@mail.com


Thanks for your help.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

file() reads an entire file to an array, each element is one line (including the linebreaks!)
you can iterate over that array, cut each line into pieces on each space there is and then use them as parameters for your query
try this one

Code: Select all

<pre>
<?php
$content = array(
		"Mary had a little lamb\n", // as mentioned file() includes any linebreak
		"Its fleece was white as snow.\n" // so I append them here, too
	);
	
foreach($content as $line)
{
	$line = trim($line); // remove the trailing linebreak
	$parts = explode(' ', $line);
	print_r($parts);
	echo $parts[0], "\n";
	echo $parts[1], "\n";
	echo $parts[2], "\n";
}
?>
</pre>
User avatar
gite_ashish
Forum Contributor
Posts: 118
Joined: Sat Aug 31, 2002 11:38 am
Location: India

Post by gite_ashish »

generally, it's better be have delimiter in field - say in our example comma (,) like -
...
Brian Smith, Brian@mail.com
Kelly Green, Kelly@mail.com
Bob Brown, Bob@mail.com
...

You can use fgetcsv() [http://in2.php.net/fgetcsv] and INSERT INTO db.
Post Reply