Search code examples
phpdatabasenetbeanssqliteopenhelper

Inputing multiple inputs into database, PHP


I need to input multiple data, as in 50000 different sets of data into a database with a txt file. The data is inputed as shown.

chr 166999824 67210768 NM_032291

They are separated with a tab.

Is there any way to help input the numerous different data without slowly inputting it in one by one? We are currently using Netbeans and MySQL to do this. The different tables are Gene_id (primary key) chromosome start_position end_position strand accession_number.

example of database

Gene_ id : 1

chromosome: chr1

start_position : 66999824

end_position: 67210768

strand : positive ( this is either positive or negative)

accession_number : NM_032291

thank you.


Solution

  • <?php
    $f=fopen('file.txt','r');
    //file function reads the entire file into an array
    $list = file($f,FILE_SKIP_EMPTY_LINES);
    
    //this is the connection to database function
    DB_connect();
    
    foreach($list as $data)
    {
        $x = explode("\t", $data);
        $chromosome=$x[0];
        $start_position=$x[1];
        $end_position=$x[2];
        $strand=$x[3];
        $accession_number=$x[4];
        $query="INSERT INTO table_name VALUES ('$chromosome','$start_position','$end_position','$strand','$accession_number')";
        mysql_query($query) or die("error insert query");
    }
    fclose($f);
    ?>