今天我写php对某个表进行插入,
之前gff_line我给它取名为 index,导致我一直产生mysql语法报错
他说明在index附近有报错,我后来才意识到index可能是mysql的标识符,在php5.5.12中不能采用index作为字段名称。
因此我把index改成另外一个名字,我的文件就能跑通了。
顺便附上我的code
<?php
function read_all_dir ( $dir )//read all the file from dir
{
$result = array();
$handle = opendir($dir);
if ( $handle )
{
while ( ( $file = readdir ( $handle ) ) !== false )
{
if ( $file != '.' && $file != '..')
{
$cur_path = $dir . DIRECTORY_SEPARATOR . $file;
if ( is_dir ( $cur_path ) )
{
$result['dir'][$cur_path] = read_all_dir ( $cur_path );
}
else
{
$result['file'][] = $cur_path;
}
}
}
closedir($handle);
}
return $result;
}
error_reporting(E_ALL &~E_NOTICE &~E_DEPRECATED);//report the mysql error
$connect = mysqli_connect('localhost','root','','dbcan3') or die('Unale to connect');
$hostdir = "D:/project/gff/cgc_table";//read all the cgc data, the data file is named by genome
$dir = read_all_dir($hostdir);
//print_r($dir);
$i = 0;
foreach ($dir['file'] as $key => $value) {
$fp = fopen($value,"r");
$str ="";
while(!feof($fp)){
$i = $i + 1;
$str = fgets($fp);
$str = trim($str);
if(strcmp($str[0],"+")!=0){//extract the non "+++" line
$arr = explode("\t",$str);
$sql_insert = "INSERT INTO cgc_new_table(gff_line,
type,prev_dis,for_dis,cgc_no,refseq_id,start,end,pro,strand,product,signature,gen,taxid,sp_name)
VALUES ('"
.mysqli_escape_string($connect,$arr[0])."','"
.mysqli_escape_string($connect,$arr[1])."','"
.mysqli_escape_string($connect,$arr[2])."','"
.mysqli_escape_string($connect,$arr[3])."','"
.mysqli_escape_string($connect,$arr[4])."','"
.mysqli_escape_string($connect,$arr[5])."','"
.mysqli_escape_string($connect,$arr[6])."','"
.mysqli_escape_string($connect,$arr[7])."','"
.mysqli_escape_string($connect,$arr[8])."','"
.mysqli_escape_string($connect,$arr[9])."','"
.mysqli_escape_string($connect,$arr[10])."','"
.mysqli_escape_string($connect,$arr[11])."','"
.mysqli_escape_string($connect,$arr[12])."','"
.mysqli_escape_string($connect,$arr[13])."','"
.mysqli_escape_string($connect,$arr[14])."');";
$result_in = mysqli_query($connect, $sql_insert) or die ('Error querying database.'. mysqli_error($connect));
if($i % 10000 == 0)
print $i."\n";
}
}
}
//$result = mysqli_query($connect, $sql) or die ('Error querying database.'. mysqli_error($connect));
?>