doute7910 2014-12-17 12:28
浏览 35
已采纳

php txt文件进入sql

I would like to take information from TXT and insert it in my database.

I want to create a loop that starts to read the first line to the two empty lines, Each line loop is independent parameter.

when the first line/line is "name" paramter And the second row is the street paramter and the third line is a city paramter and the rest of the remaining lines are text parameter.

It is important to note that not all loops equal number of their lines and what separates each loop is 2 blank lines.

<?php

$file = file('try1.txt');  

for ($i = 0; $i < count($file); $i++) {

  $sql = "INSERT INTO `table` (`name`,`street`,`city`,`text`) VALUES ('$name','$street','$city','$text')"; 
        mysql_query($sql);
}
?>

the file: try1.txt

  • 写回答

2条回答 默认 最新

  • dqqy64515 2014-12-17 12:50
    关注

    Do you want something like this?

    $content = "Name
    Street
    City
    This
    is
    some
    text
    
    
    Name2
    Street2
    City2
    Something
    else
    string";
    
    //$content = file_get_contents("try1.txt");
    //$link = mysqli_connect("localhost", "dbuser", "dbpass", "dbname");
    $string = str_replace("", '', $content);
    $blocks = explode("
    
    
    ", $string);
    foreach ($blocks as $block) {
        $lines = explode("
    ", $block);
        $name = $city = $street = $text = "";
        if (!empty($lines[0])) {
            $name = $lines[0];
        }
        if (!empty($lines[1])) {
            $street = $lines[1];
        }
        if (!empty($lines[2])) {
            $city = $lines[2];
        }
        if (!empty($lines[3])) {
            for ($i=3; $i < count($lines); $i++) {
                $text .= " " . $lines[$i];
            }
            $text = ltrim($text," ");
        }
        var_dump ($name);
        var_dump ($street);
        var_dump ($city);
        var_dump ($text);
        echo '----------------------------'."<br />";
        /*
        $sql = "INSERT INTO `table` (`name`, `street`, `city`, `text`)"
            . " VALUES ('".mysqli_real_escape_string($link, $name)."',"
            . "'".mysqli_real_escape_string($link, $street)."',"
            . "'".mysqli_real_escape_string($link, $city)."',"
            . "'".mysqli_real_escape_string($link, $text)."')";
        mysqli_query($link, $sql);
         * 
         */
    }
    

    Output is

    string 'Name' (length=4)
    string 'Street' (length=6)
    string 'City' (length=4)
    string 'This is some text' (length=17)
    ----------------------------
    string 'Name2' (length=5)
    string 'Street2' (length=7)
    string 'City2' (length=5)
    string 'Something else string' (length=21)
    ----------------------------
    

    NOTE:
    So what you need to do:

    • Remove the $content and uncomment that row, where file_get_contents is.

    • Uncomment the $link row, and set your connection parameters.

    • Remove var_dumps, and echo "-----" lines.

    • Uncomment the rest.

    In this example, I've used the mysqli as procedural style, check the document for PDO or mysqli how to use them OOP style.

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

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么