逐行比较 sql table 与 txt 文件与 table 如果变量 dosent exsit 插入它

Compare sql table with txt file line by line with table if variable dosent exsit insert it

<?php
function db_query()
    {
        $servername = "localhost";
        $username = "root";
        $password = "";
        $dbname = "single4thenight";

        // Create connection
        $conn = new mysqli($servername, $username, $password, $dbname);
        // Check connection
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        } 

        $sql = "SELECT id, name, alias, type, parent, ordering, published FROM iutca_jomcl_locations";  //selects locations from 
        $result = $conn->query($sql);

        if ($result->num_rows > 0) {
            // output data of each row
            while($row = $result->fetch_assoc()) {
                echo "id: " . $row["id"]. " - Name: " . $row["name"]. "  alias" . $row["alias"]. "  type: " . $row["type"]. " - parent: " . $row["parent"]. " ordering " . $row["ordering"]. "published " . $row["published"]."<br>";
            }
        } else {
            echo "0 results";
        }
        $conn->close();

    }
function read_location()
    {
        $servername = "localhost";
        $username = "root";
        $password = "";
        $dbname = "single4thenight";

        // Create connection
        $conn = new mysqli($servername, $username, $password, $dbname);
        // Check connection
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        } 

        $sql = "SELECT name FROM iutca_jomcl_locations";  //selects locations from 
        $result = $conn->query($sql);

        if ($result->num_rows > 0) 
        {
            while($row = $result->fetch_assoc()) {
            //  echo "Location : " . $row["name"]."<br>";
                $row_name = $row["name"];
                echo $row_name.'<br />';
            }

        }
        $file1 = "./location.txt";
        $lines = file($file1);
        foreach($lines as $line_num => $line)
        {
        echo $line;

        }

    }

我的 location.txt 文件包含这个

奥本 伯明翰 多森 加兹登 亨茨维尔 移动的 蒙哥马利 肌肉浅滩 塔斯卡卢萨

我想将我的 sql 数据库与 txt 文件进行比较,以确保我确实没有内部变量。我不想在 sql 旁边放置重复项 我想知道更新 sql

的最简单方法是什么

您可以使用 INSERT IGNORE INTO 而不仅仅是 INSERT INTO,然后 MySQL 将忽略重复的条目。有关详细信息,请参阅 MySQL documentation for INSERT。所以,根据我在你的问题中看到的内容,你的 SQL 看起来像:

INSERT IGNORE INTO iutca_jomcl_locations ('name') values (?)

希望对您有所帮助! :)

首先我们将文件内容读入$content变量

$content = file('mytxt.txt')

正如您发布的那样,您的文件包含用 space 分隔的单词(如果没有,请跳过此步骤并使变量 $words 包含您需要的值)因此我们需要拆分内容,以将每个单词作为数组项

$words = explode(" ", $content);

最后,插入值并检查数据库中是否存在类似的值

foreach($words as $word)
{
  $sql = "INSERT  iutca_jomcl_locations (name) 
  SELECT  $word
  WHERE   NOT EXISTS 
          (   SELECT  1
              FROM    tblSoftwareTitles 
              WHERE   name = $word
          );"
  $result = $conn->query($sql);
}

iutca_jomcl_locations - table 姓名
name - 要插入的列(也使用此列检查唯一值)

我使用了此代码并且对我有用

 foreach($lines as $line_num => $line)
        {
            $line = $line;
            //echo $line;
            $sql = "SELECT ordering, name FROM iutca_jomcl_locations WHERE name='$line'";
            $result = $conn->query($sql);

        if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
        //echo "id: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
        echo "Found Locations: " . $row["name"]." The ordering Number Is " . $row["ordering"]."<br>";
        $ordering =$row["ordering"];
        }
        } else {

                if($ordering >=0)
                {
                    $count = $ordering;
                    //echo "0 results";
                    $lowerCase = strtolower($line);
                    $sql = "INSERT INTO iutca_jomcl_locations (name, alias , parent, published,ordering)
                    VALUES ('$line','$lowerCase','$parent','1','$count')";


                    $count = $count + 1;
                    if ($conn->query($sql) === TRUE) {
                    echo "New record created successfully <br />";
                    } else {
                    echo "Error: " . $sql . "<br>" . $conn->error;
                    }
                }
        }

        }