Forum Moderators: coopster

Message Too Old, No Replies

importing text file into database?

improting text file into mysql database

         

websterz

8:16 pm on Jan 30, 2010 (gmt 0)

10+ Year Member



i want to ask for some help from the experts,
actually i have some data in text file which have the following format:

johnsmith
johnybravo
heeelooo
bravoman
terminator
spykids

now i want to import it to mysql database through php how can i do it by simple script? any help will be appreciated. thanks

Alcoholico

8:52 pm on Jan 30, 2010 (gmt 0)

10+ Year Member



If your file is not huge, then you could use something like:
<?php
... connect to your database
$arr_names = file(PATH_AND_NAME_OF YOUR_FILE);
$query = 'INSERT INTO `NAME_OF_YOUR_TABLE` (`contact_name`) VALUES ';
foreach($arr_names as $name) {
$name = mysql_real_escape_string($name);
$query .= "('$name'),";
}
$query = rtrim($query, ',');

And the rest here....
?>


Of course, you could also use a tool such as phpMyAdmin to import your text file.

websterz

1:27 pm on Jan 31, 2010 (gmt 0)

10+ Year Member



I have done it ;) by using another way.

thanks alcoholic brother :)