简体   繁体   English

将多个输入输入数据库,PHP

[英]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. 我需要输入多个数据,例如使用txt文件将5万种不同的数据集输入数据库。 The data is inputed as shown. 如图所示输入数据。

chr 166999824 67210768 NM_032291 hr 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. 我们当前正在使用Netbeans和MySQL来完成此任务。 The different tables are Gene_id (primary key) chromosome start_position end_position strand accession_number. 不同的表是Gene_id(主键)染色体start_position end_position链accession_number。

example of database 数据库示例

Gene_ id : 1 基因ID:1

chromosome: chr1 染色体:chr1

start_position : 66999824 开始位置:66999824

end_position: 67210768 结束位置:67210768

strand : positive ( this is either positive or negative) 链:正(这可以是正或负)

accession_number : NM_032291 accession_number:NM_032291

thank you. 谢谢。

your codes will look like this. 您的代码将如下所示。

$file = fopen("yourtextfile.txt","r");
    while(! feof($file))
      {
      $line = fgets($file);
      $array = preg_split('/\s+/', $line); // split lines by tab or spaces
      // your query here Ex Insert into tableName(col1,col2,col2)values($array[0],$array[1],$array[2]
      }
    fclose($file);
<?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);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM