user3789191
user3789191

Reputation: 97

Mysql restore from file

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|

Upvotes: 0

Views: 51

Answers (1)

baao
baao

Reputation: 73281

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

Upvotes: 1

Related Questions