使用PHP将多个文本文件导入数据库表单元格 [英] Import multiple text files to database table cells using PHP
问题描述
我正在尝试将多个文本文件的内容传输到MySQL中。这些文件按顺序命名为 1.txt
, 2.txt
, 3.txt
... 13381.txt
。基本上,每个文件都是一个HTML文档,没有所有标题,元数据等被删除,只剩下内容。
我还做了一个表列 ID
和 CONTENT
。 ID
是自动递增的。我希望这些文件的内容进入 CONTENT
列的单元格,如下所示:
1.txt - >第1行
2.txt - >第2行
3.txt - >第3行
等
搜索周围只会导致我从CSV和其他表格导入数据数据形式。我总是可以打开每个文件并从那里复制粘贴到类似phpmyadmin的东西,但这似乎是一件单调乏味的事情,因为有数以千计的数据文件需要通过。
<我想知道是否有一种简单的方法可以使用PHP和某种递归循环来获取文件内容,然后更新数据库。我已经进行了一些实验,但我似乎已经超越了平庸的编程技能。有人在乎帮忙吗?在此先感谢:)
使用PDO这样的事情......
<?php
$ host ='localhost'; $ db ='database-name'; $ user ='database-user-name'; $ pw ='database-password';
$ conn = new PDO('mysql:host ='。$ host。'; dbname ='。$ db。'; charset = utf8',$ user,$ pw);
$ conn-> setAttribute(PDO :: ATTR_ERRMODE,PDO :: ERRMODE_EXCEPTION);
$ i = 0;
do {
$ i = $ i + 1;
$ txt = file_get_contents($ i.txt);
try {
$ sql =INSERT INTO table(CONTENT)VALUES(:txt);
$ stmt = $ conn-> prepare($ sql);
$ stmt-> bindValue(':txt',$ txt,PDO :: PARAM_STR);
$ stmt-> execute();
} catch(PDOException $ e){
die(无法完成插入。$ i。。txt。$ e-> getMessage()); //这将告诉你哪些文本文件导致错误
}
} while($ i< = 13381); // close for loop
echo('All Done!');
?>
希望这会有所帮助。
I'm trying to transfer the contents of multiple text files into a MySQL. These files are sequentially named 1.txt
, 2.txt
, 3.txt
... 13381.txt
. Basically, each file is an HTML document without all the headers, meta, etc. that's been stripped away and only the content is left.
I've also made an table with a columns ID
and CONTENT
. ID
is auto-incrementing. I'd like the contents of the files to go into the CONTENT
column's cells like this:
1.txt -> row 1
2.txt -> row 2
3.txt -> row 3
etc.
Searching around only leads me to importing data from CSVs and other tabular forms of data. I could always just open each file and copy-paste from there into something like phpmyadmin but that seems like a tedious thing to do given that there's thousands of data files to go through.
I was wondering if there was an easy way to do this using PHP and some kind of recursive loop that gets file contents and then UPDATEs the database. I've experimented a bit but I've seem to come against a wall beyond my mediocre programming skills. Anyone care to help? Thanks in advance :)
Something like this using PDO...
<?php
$host = 'localhost'; $db = 'database-name'; $user = 'database-user-name'; $pw = 'database-password';
$conn = new PDO('mysql:host='.$host.';dbname='.$db.';charset=utf8', $user, $pw);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$i = 0;
do {
$i = $i+1;
$txt = file_get_contents("$i.txt");
try {
$sql = "INSERT INTO table (CONTENT) VALUES (:txt)";
$stmt = $conn->prepare($sql);
$stmt->bindValue(':txt', $txt, PDO::PARAM_STR);
$stmt->execute();
} catch (PDOException $e) {
die("Could not complete the insert for ".$i.".txt " . $e->getMessage()); // this will tell you which of your text files caused the error
}
} while ($i <= 13381); // close for loop
echo('All Done!');
?>
Hope this helps.
这篇关于使用PHP将多个文本文件导入数据库表单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!