读取文本文件并将内容传输到mysql数据库 [英] Read a text file and transfer contents to mysql database
本文介绍了读取文本文件并将内容传输到mysql数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要一个php脚本来读取.txt文件。
文本文件的内容如下:
data.txt
145 | Joe Blogs | 17/03/1954
986 | Jim Smith | 12/01/1976
234 | Paul Jones | 19/07/1923
098 | James Smith | 12/09/1998
234 | Carl Jones | 01/01/1925
** DataID | Name |
DOB **234 | Carl Jones | 01/01/1925
pre>
如果有人可以给我脚本来实现这一点,我会非常感激。
更新:
< ;?
$ handle = @fopen(data.txt,r);
$ conn = mysql_connect(localhost,username,password);
mysql_select_db(mydatabase,$ conn);
while(!feof($ handle))//循环直到文件结束。
{
$ buffer = fgets($ handle,4096);
//读一行。
list($ a,$ b,$ c)= explode(|,$ buffer);
//用|的方法分隔字符串
echo $ a。 - 。$ b。 - 。$ c。< br>;
$ sql =INSERT INTO data_table(iddata,name,age)VALUES('。$ a。','。$ b。',。$ c。
mysql_query($ sql,$ conn)或die(mysql_error());
}
?>
在SQL语法中获取以下错误错误: ...在第1行使用正确的语法来使用')'
解决方案使用fopen打开txt文件:
$ handle = @fopen(xyz.txt,r); //逐行读取
$ values ='';
while(!feof($ handle))//循环直到文件结束。
{
$ buffer = fgets($ handle,4096); //读一行。
list($ a,$ b,$ c)= explode(|,$ buffer); //用|
//values.=($a,$b,$c);//保存值,最后使用insert query或
//使用mysql插入查询
}
这是
I need a php script to read a .txt file.
The content of the text file are like this:
data.txt
145|Joe Blogs|17/03/1954 986|Jim Smith|12/01/1976 234|Paul Jones|19/07/1923 098|James Smith|12/09/1998 234|Carl Jones|01/01/1925
These would then get stored into a database like this
**DataID |Name |DOB **
234 |Carl Jones|01/01/1925
I would be so grateful if someone could give me script to achieve this.
Update:
<? $handle = @fopen("data.txt", "r"); $conn = mysql_connect("localhost","username","password"); mysql_select_db("mydatabase",$conn); while (!feof($handle)) // Loop til end of file. { $buffer = fgets($handle, 4096); // Read a line. list($a,$b,$c)=explode("|",$buffer); //Separate string by the means of | echo $a."-".$b."-".$c."<br>"; $sql = "INSERT INTO data_table (iddata, name, age) VALUES('".$a."','".$b."',".$c.")"; mysql_query($sql,$conn) or die(mysql_error()); } ?>
get the following error error in your SQL syntax; ...for the right syntax to use near ')' at line 1
解决方案open txt file using fopen:
$handle = @fopen("xyz.txt", "r"); //read line one by one $values=''; while (!feof($handle)) // Loop til end of file. { $buffer = fgets($handle, 4096); // Read a line. list($a,$b,$c)=explode("|",$buffer);//Separate string by the means of | //values.=($a,$b,$c);// save values and use insert query at last or // use mysql insert query here }
THATS IT
这篇关于读取文本文件并将内容传输到mysql数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文