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

时间:2018-09-25 18:23:13

标签: 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);

}

?>

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

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:2)

代替逐行读取,只需使用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();
   }
}