如何使用数组恢复MySQL表?

时间:2014-04-12 21:40:28

标签: php mysql

我意外地犯了一个可怕的错误,现在我的整个桌子都没了。幸运的是,这是我出口的第一张桌子,所以我有一个备份。但我已将其导出为.php,并以这种方式导出:

$vocabulary = array(
  array('Language' => 'Gotisch','Nederlands' => 'Gotisch','German' => 'Gotisch','English' => 'Gothic','article' => '','id' => '0','type' => '','French' => ''),
  array('Language' => 'aug´','Nederlands' => 'oog','German' => 'Auge','English' => 'eye','article' => ' ','id' => '0','type' => '','French' => ''))

有没有办法可以在MySQL中重新插入它?因为它似乎不是.CSV或.XML,换句话说,我可以将其转换为其中一种文件类型吗?

2 个答案:

答案 0 :(得分:1)

这将为您创建一个CSV文件。

<?php
$vocabulary = array(
    array('Language' => 'Gotisch','Nederlands' => 'Gotisch','German' => 'Gotisch','English' => 'Gothic','article' => '','id' => '0','type' => '','French' => ''),
    array('Language' => 'aug´','Nederlands' => 'oog','German' => 'Auge','English' => 'eye','article' => ' ','id' => '0','type' => '','French' => ''));

$fp = fopen('file.csv', 'w');
foreach($vocabulary as $row)
{
    fputcsv($fp, $row);
}

fclose($fp);
?>

答案 1 :(得分:1)

你不能循环浏览它并查询所有内容吗?

<?php
$vocabulary = array(
  array('Language' => 'Gotisch','Nederlands' => 'Gotisch','German' => 'Gotisch','English' => 'Gothic','article' => '','id' => '0','type' => '','French' => ''),
  array('Language' => 'aug´','Nederlands' => 'oog','German' => 'Auge','English' => 'eye','article' => ' ','id' => '0','type' => '','French' => ''));

foreach($vocabulary as $word) {
    $sql = "INSERT INTO tbl (Language, Nederlands, German, English, article, id, type, French) VALUES ('" . $word['Language'] . "', '". $word['Nederlands'] . "', '" . $word['German'] . "', '" . $word['English'] . "', '" . $word['article'] . "', '". $word['id'] . "', '" . $word['type'] . "', '" . $word['French'] . "')";
    //query $sql here. also change tbl to your table name
}

?>