Importing to Mysql from TXT

1

I have a txt file coming from a database that separates items with a space of type TAB, the item is first the product name, the second txt item is the value, type this, I need to know how to do php import in this way by separating the fields correctly, what can you tell me to study as directly as possible some tutorial for example?

Language: PHP

    
asked by anonymous 01.02.2015 / 05:36

1 answer

2

Basic solution

 // Abrir documento fonte
 $name_src = "nome_documento.txt";
 $h_src = fopen($name_src,"r");


 // Leitura linha depois linha até o final
 while (($str_src = fgets($h_src)) !== false)
 {

    // Explode sobre TAB
    $tab = explode("\t",$str_src);

    // Prepara o query (exemplo com 3 valors per linha)
     $query_insert = "INSERT INTO TAB_GARDE_ACT "
     ."( "
     ."campo1, "
     ."campo2, "
     ."campo3 "
     .") "
     ."VALUES "
     ."("
     ."'".$tab[0]."', "
     ."'".$tab[1]."', "
    ."'".$tab[2]."' "
    .") ";

     //Fazer o query (depende do que vc usa Mysql, mysqli, dbp, postgress...)
     $result = sql_query($connector,$query_insert);
 }

 // Fechar documento
 fclose($h_src);

You need (normal!) to open the connection to the BDD. If you have a lot of data, you can use a SQL call only. To do this, you need to use mysql_insert_array ().

    
01.02.2015 / 12:49