mysql中如何读取txt文件内容,读取文本文件并将内容传输到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."
";

$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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值