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.
How do I add a list of emails to my database?
Moderator: General Moderators
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
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>- gite_ashish
- Forum Contributor
- Posts: 118
- Joined: Sat Aug 31, 2002 11:38 am
- Location: India
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.
...
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.