php读取txt文件写入数据库问题

来源:百度知道 编辑:UC知道 时间:2024/05/16 18:48:00
例如:1.txt文件
555
666
777
-
5555
6666
7777
-
555555
666666
777777
等3条记录信息,如何读取后写入数据库

<?php
$file_dir="1.txt";
$fp=fopen($file_dir,"r");
$content=fread($fp,filesize($file_dir));//读文件
fclose($fp);

function replaces($str)//为各字段添加''
{
foreach($str as $k=>$v)
{
$str[$k]="'".trim($v)."'";
}
return $str;
}

function Get_item($tmp1,$tmp2,$cont,$sq)//取得sql语句;tmp1为记录分割点,tmp2为字段分割点
{
$tmp_rows=explode($tmp1,$cont);
foreach($tmp_rows as $key=>$value)
{
$tmp_rows2[$key]=replaces(explode($tmp2,trim($value)));
$sql[$key]=$sq.implode(",",$tmp_rows2[$key]).")\"";
}
return $sql;
}
$sq="\"insert into table1 (a1,a2,a3) values (";//sql的前半部分,要完善!
$sql=array();
$sql=Get_item("-"," ",$content,$sq);
//$db=mysql_connect("localhost","root","abc");//联接mysql,自己加
//mysql_sele