/    Sign up×
Community /Pin to ProfileBookmark

import a txt file to a mysql database

Hi

How can i import a txt file to a mysql database where each column ends using the & character ?
eg.:
texthere&othertexthere&anothertexthere&texthere&texthere
writing a php script file ?

or:

It can be done using phpmyadmin ? how it is done in phpmyadmin

thank´s in advance

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYOct 21.2005 — do you want to create a table with each field named after the text between the ampersand sign?

or does your database structure already exist and you want to fill it with the text between the ampersand sign?
Copy linkTweet thisAlerts:
@NogDogOct 21.2005 — Does the table already exist, or do you have to create it "on the fly"?

Will each line in the file have the same number of fields, or can it vary; and will those fields always be in the same order on each line?

If yes to all the above, the below should work. If no, then we'll have to be more creative.
[code=php]
#### THIS IS UNTESTED, SO NO GUARANTEES ####
$data = file("data.txt") or die("Could not read data file");
$connx = mysql_connect('yadda', 'yadda', 'yadda') or
die("Unable to connect to mysql");
$db = mysql_select_db('database') or die("Could not select DB");
$count = 0;
foreach($data as $line)
{
$fields = explode('&', $line);
if(count($fields) == 5) # have the right number of fields
{
$query = "INSERT INTO table_name (col1,col2,col3,col4,col5)";
$query .= " VALUES ('" implode("','", $fields) . "')";
$result = @myql_query($query);
if($result)
{
$count += mysql_affected_rows($result);
}
else
{
echo "<p>QUERY FAILED: $query - " . mysql_error() . "</p>n";
}
}
else
{
echo "<p>INVALID DATA LINE: '$line'</p>n";
}
}
echo "<p>$count rows were inserted into the table.</p>n";
[/code]
Copy linkTweet thisAlerts:
@amrigoauthorOct 21.2005 — The table already exist.

Each line in the file have the same number of fields.

Those fields will always be in the same order on each line:

eg.:

000001.03.02-04/5&PAUL LOPES&Auditory of Santa Monica/SP&003/02-CRPO/CENTRAL&INTIMACAO DA DEFESA&In:2003/11/21

I will try the example posted by Nog Dog

And just to let me know: phpmyadmin can be used to do it ? i try but i did not discover yet the right way to do it.
Copy linkTweet thisAlerts:
@amrigoauthorOct 21.2005 — Hi

Thank´s a lot the script is working

i will now add some validation to ensure the file have the required format and all the lines are well formated

Best regards !!
Copy linkTweet thisAlerts:
@NogDogOct 21.2005 — Sounds good, glad it worked. ?
×

Success!

Help @amrigo spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...