i want to import wa txt file contains 5000 mail to mysql dabase but i need an easy way not to insert the 5000 mail line by line
| SitePoint Sponsor |
i want to import wa txt file contains 5000 mail to mysql dabase but i need an easy way not to insert the 5000 mail line by line
Insert one record manually using phpmyadmin, and see what SQL statement is generated to enter the record. Then do a search and replace in your text file to make all the 5000 lines as per a similar structure. Copy and paste this code in Phpmyadmin->SQL
If you have a CSV style formatting in your file it should be easy enough to load with LOAD DATA INFILE.
And even if it's not, I'm sure you can craft a simple PHP script to reformat the text document line after line.
Post us a line of your document, obscure the data, maybe we can give a hint.
Programming boils down to three things: fast, good and cheap.
Please pick two.
Or, if you want, you could store every line in your file to an array, and use a foreach statement to loop through the emails and add the query to a string, for example:
Or if you don't want to run the query, but just see it (to check it over), echo $query_string instead of querying it.PHP Code:<?
$email_string = file_get_contents("emails.txt");
$email_array = explode("\n", $email_string);
$query_string = "INSERT INTO `emails` (`email`) VALUES ";
foreach($email_array as $line => $email_address){
if($line > 0){
$query_string .= ", ";
}
$query_string .= "('".$email_address."')";
}
mysql_query($query_string);
Last edited by Jake Arkinstall; Aug 27, 2007 at 02:29. Reason: Forgot to put single quotes on adding email address to query
Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
LOAD DATA INFILE is supposed to work a lot faster and be used for loading a large number of records in a DB, as opposed to inserting each one.
At 5k records, say processing and inserting each and every one takes 1ms, and you still have a 5s processing time. Some hosters timeout their PHP scripts at 2s.
Programming boils down to three things: fast, good and cheap.
Please pick two.
Bookmarks