0
<?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 的最简单方法是什么

4

3 回答 3

1

您可以使用INSERT IGNORE INTO而不是 just INSERT INTO,然后 MySQL 将忽略重复的条目。有关更多信息,请参阅INSERT 的 MySQL 文档。因此,根据我在您的问题中看到的内容,您的 SQL 将类似于:

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

希望这可以帮助!:)

于 2015-06-05T18:51:17.070 回答
1

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

$content = file('mytxt.txt')

正如您发布的那样,您的文件包含用空格分隔的单词(如果没有,请跳过此步骤并使变量 $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- 表名
name- 要插入的列(也使用此列检查唯一值)

于 2015-06-05T18:59:50.697 回答
0

我使用了此代码,它对我有用

 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;
                    }
                }
        }

        }
于 2015-06-07T19:32:13.240 回答