【问题标题】:How to read a text file and insert those values into database using php如何使用 php 读取文本文件并将这些值插入数据库
【发布时间】:2013-12-31 09:26:16
【问题描述】:

我有一个像这样命名为 ernakulam.txt 的文本文件,其内容如下:

name1
name2
name3
name4
name5

我想将这个值插入到名为places 的Mysql 数据库表中。 Mysql 表有这样的字段:

dist_id  |  name
_________|______
         |
         |

在此表中,field1 (dist_id) 对于所有行都应该相同。 dist_id 值为 6。如何从文本文件中读取这些名称并使用 php 插入到 Mysql 表中?

【问题讨论】:

  • 你的尝试?你已经完成的任何代码
  • 请尝试while($dat=fread($res));
  • 你可以使用file(),然后用线循环数组,修剪它们并使用PDO将它们插入数据库

标签: php mysql text


【解决方案1】:
<?php

$file = '/path/to/ernakulam.txt';
$file_content = file_get_contents($file);
$names = explode("\n", $file_content);

等等

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多