0

I have Database table saved in a txt file with the format of the following

id | name | score | reward | result | note
id | name | score | reward | result | note
id | name | score | reward | result | note

I would like to put these database to the mysql database table. Can it be done by using PHP?

what about if the format is

|id | name | score | reward | result | note|
|id | name | score | reward | result | note|
|id | name | score | reward | result | note|
1
  • 1
    yes its quite possible, google open text file php. you can start from that Commented Nov 13, 2014 at 4:31

1 Answer 1

1

This will help you:

 $file  = file('file.txt');

foreach ($file as $line) 
{ 
  $column = explode("|", $line);

$a = $column[0];
$b = $column[1];
$c = $column[2];
$d = $column[3];
$e = $column[4];
$f = $column[5];
$query = "INSERT INTO table (a,b,c,d,e,f) VALUES ('$a','$b','$c','$d','$e','$f');
mysqli_query($sql,$query);
}

You'll want to use prepared statements instead, and you need to alter the insert depending on your db connection... Of course you need to change table and table columns to your needs

Sign up to request clarification or add additional context in comments.

1 Comment

I think in the second case you'll need to change $a = $column[1]; and so on. You can try it out by just echoing $column[1] and so on instead of inserting directly

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.