dongri1989 2015-06-05 18:26
浏览 11
已采纳

将sql表与txt文件逐行比较,如果变量不存在则将其插入[关闭]

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

        }

    }

My location.txt file contains this

Auburn Birmingham Dothan Gadsden Huntsville Mobile Montgomery Muscle Shoals Tuscaloosa

I would like to compare my sql database with txt file to make sure that i do not arealdy have variables inside. I do not want to put duplicates in side my sql i would like to know what is the easiest way to update my sql

  • 写回答

3条回答 默认 最新

  • dqb77047 2015-06-05 18:51
    关注

    You could use INSERT IGNORE INTO instead of just INSERT INTO and MySQL will then ignore duplicate entries. See the MySQL documentation for INSERT for more information. So, based on what I see in your question, your SQL would look something like:

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

    Hope this helps! :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大