php - 通过php将多个文件的数据插入mysql数据库

标签 php mysql

您好,我正在尝试通过 php 脚本将文件夹内多个文件的数据插入到 mysql 数据库

它们大约有 750 个文件,位于我的服务器上一个名为 Notes 的文件夹中,我使用了以下代码

每个文件具有以下结构

<starttitle>Title</div>
<start>
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.

The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.
<end>

我使用以下 php 代码循环遍历文件并插入数据库

<?php

$con = mysqli_connect("localhost","root","","qbank");

// Check connection
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}



function get_string_between($string, $start, $end){
    $string = ' ' . $string;
    $ini = strpos($string, $start);
    if ($ini == 0) return '';
    $ini += strlen($start);
    $len = strpos($string, $end, $ini) - $ini;
    return substr($string, $ini, $len);
}

            foreach (glob("notes/*.php") as $file) {
                $file_handle = fopen($file, "r");
                while (!feof($file_handle)) {
                    $line = fgets($file_handle);
                    $title = get_string_between($line, '<starttitle>', '</div>');
                    $content = get_string_between($line, '<start>', '<end>');
                    $date = date('Y-m-d H:i:s');
                    $by = "Marco";


                    if(!empty($title) || !empty($content)){

                        $stmt = $con->prepare("INSERT INTO textbook (textbook_address, text, created_at, created_by) VALUES (?, ?, ?, ?)");
                        $stmt->bind_param("ssss", $title, $content, $date, $by);
                        $stmt->execute();
                        $stmt->close();

                    }

                }
                fclose($file_handle);

}

?>

问题是此代码仅在一行中插入标题并将文本放在下一行,但我需要将每个标题及其自己的文本放在同一行中

任何帮助将不胜感激

最佳答案

不用逐行读取,只需使用 file_get_contents (如果它是脏脚本)。然后从结果中删除数据,然后执行查询。所有这些 foreach 文件。

像这样:

foreach (glob("notes/*.php") as $file) {
  $data = file_get_contents($file);
  $title = get_string_between($data, '<starttitle>', '</div>');
  $content = get_string_between($data, '<start>', '<end>');
  $date = date('Y-m-d H:i:s');
  $by = "Marco";
  if(!empty($title) || !empty($content)){
    $stmt = $con->prepare("INSERT INTO textbook (textbook_address, text, created_at, created_by) VALUES (?, ?, ?, ?)");
    $stmt->bind_param("ssss", $title, $content, $date, $by);
    $stmt->execute();
    $stmt->close();
   }
}

关于php - 通过php将多个文件的数据插入mysql数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52504571/

相关文章:

mysql - 如果太长停止 mysql 查询

mysql - 无法使用 mysql db 在 rails 中启动服务器

php - 在 PHP 中回显提交按钮

php - 如果成本可变,如何计算给定时间内销售订单的利润?

php - laravel-echo-server 404 尝试进行身份验证时

php - Xcode 将带 url 的变量发送到 php 页面,为 select 语句检索 php 代码中的变量

mysql - 如何在 MyISAM 中没有指定引擎的情况下导入 mysqldump

php - 统计mysql表中多列总和等于0的记录条数

php - PostgresQL从多维数组类型的Json字段中取数据

javascript - 如何从 javascript 中获取 Select HTML 中正确选择的值?