Page 1 of 1

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

Posted: Wed Oct 29, 2003 9:04 am
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.

Posted: Wed Oct 29, 2003 9:21 am
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>

Posted: Wed Oct 29, 2003 12:46 pm
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.