load data from txt to mysql db

eduardo12fox
eduardo12fox used Ask the Experts™
on
I have a file in the format

using notepad + + to see the need to get data from column 13 to 31 and column 36 to 46 and 96 to 120

need to do it line by line to insert into mysql db

help me!
#DCC##EMB#0 052 551800003200 9"    28/04/1990   3-EMPRESARIAL     ENFERMARIA        31/12/2012"ANA PAULA AGUIAR PEREIRA      01"0052    AMB-ENF-OBS          GRUPO DE MUNICIPIOS NAO HA            INGRESSO TOTAL    "#ENC#ANA PAULA AGUIAR PEREIRA ;00525518000032009=011212=005205643000?#END#@@@@@@PRODUTO REGULAMENTADO     440013025
#DCC##EMB#0 052 551800003300 5"    18/07/1988   3-EMPRESARIAL     ENFERMARIA        31/12/2012"NATHALIA PINHEIRO COSTA       01"0052    AMB-ENF-OBS          GRUPO DE MUNICIPIOS NAO HA            INGRESSO TOTAL    "#ENC#NATHALIA PINHEIRO COSTA  ;00525518000033005=011212=005205643000?#END#@@@@@@PRODUTO REGULAMENTADO     440013025
#DCC##EMB#0 052 551800003400 1"    06/02/1984   3-EMPRESARIAL     ENFERMARIA        31/12/2012"SUELEN BRAGA                  01"0052    AMB-ENF-OBS          GRUPO DE MUNICIPIOS NAO HA            INGRESSO TOTAL    "#ENC#SUELEN BRAGA             ;00525518000034001=011212=005205643000?#END#@@@@@@PRODUTO REGULAMENTADO     440013025
#DCC##EMB#0 052 551800003500 8"    08/05/1989   3-EMPRESARIAL     ENFERMARIA        31/12/2012"LUIZ EDUARDO C GONCALVES      01"0052    AMB-ENF-OBS          GRUPO DE MUNICIPIOS NAO HA            INGRESSO TOTAL    "#ENC#LUIZ EDUARDO C GONCALVES ;00525518000035008=011212=005205643000?#END#@@@@@@PRODUTO REGULAMENTADO     440013025
#DCC##EMB#0 052 551800003600 4"    19/07/1991   3-EMPRESARIAL     ENFERMARIA        31/12/2012"EVELYN DA SILVA ANDRADE       01"0052    AMB-ENF-OBS          GRUPO DE MUNICIPIOS NAO HA            INGRESSO TOTAL    "#ENC#EVELYN DA SILVA ANDRADE  ;00525518000036004=011212=005205643000?#END#@@@@@@PRODUTO REGULAMENTADO     440013025
#DCC##EMB#0 052 551800003700 0"    02/01/1986   3-EMPRESARIAL     ENFERMARIA        31/12/2012"IVAN SERGIO M DE PAULA        01"0052    AMB-ENF-OBS          GRUPO DE MUNICIPIOS NAO HA            INGRESSO TOTAL    "#ENC#IVAN SERGIO M DE PAULA   ;00525518000037000=011212=005205643000?#END#@@@@@@PRODUTO REGULAMENTADO     440013025
#DCC##EMB#0 052 551800003800 7"    17/12/1988   3-EMPRESARIAL     ENFERMARIA        31/12/2012"JULIANA E V DE SOUZA          01"0052    AMB-ENF-OBS          GRUPO DE MUNICIPIOS NAO HA            INGRESSO TOTAL    "#ENC#JULIANA E V DE SOUZA     ;00525518000038007=011212=005205643000?#END#@@@@@@PRODUTO REGULAMENTADO     440013025

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
<?php
$lines = file('filename.txt', FILE_SKIP_EMPTY_LINES);
foreach($lines as $line) {
    $str1 = substr($line, 13, 31);
    $str2 = substr($line, 36, 46);
    $str3 = substr($line, 96, 120);
    $sql = "insert into table_name(field1, field2, field3) values ('".$str1."', '".$str2."', '".$str3."')";
    $result = mysql_query ($sql);
}
?>
Most Valuable Expert 2011
Top Expert 2016

Commented:
In addition to what kivan24 showed, you might also want to learn about these functions:
http://php.net/manual/en/function.mysql-real-escape-string.php
http://php.net/manual/en/function.mysql-error.php

Author

Commented:
Perfect greatest!!! Thanks.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial