我需要输入多个数据,就像在一个带有txt文件的数据库中输入50000个不同的数据集一样。数据的输入如图所示。
chr 166999824 67210768 NM_032291
它们用标签隔开。
有没有什么方法可以帮助输入大量不同的数据,而不需要一个接一个地慢慢输入?我们目前正在使用Netbeans和MySQL来实现这一点。不同的表格Gene_id(主键)染色体起始位置结束位置搁浅addition_number。
数据库示例
基因id:1
染色体:chr1
起始位置:66999824
结束位置:67210768
链:阳性(这是阳性或阴性)
材料编号:NM_032291
谢谢。
<?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);
?>
您的代码将如下所示。
$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);