美文网首页
数据库批量插入操作

数据库批量插入操作

作者: keaidelele | 来源:发表于2017-06-08 16:26 被阅读65次
    <?php
    /**
     * Created by PhpStorm.
     * User: huangle
     * Date: 2017/6/8
     * Time: 7:12
     */
    require("config/db.php");
    
    $sql = "CREATE TABLE pdb_middle (
    pm_id INT(50) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    hitid VARCHAR(20),
    description VARCHAR(500)
    )";
    if($linkID->query($sql)==TRUE){
        echo "Table result_cdd created successfully";
    }else{
        echo "created wrong!";
    }
    $sql = "";
    $file_path = "D:/project/annotation-id/pdbmiddle";
    $fp = fopen($file_path,"r");
    $str= file_get_contents($file_path);
    $arr=explode("\n",$str);
    for ($i = 0;$i<sizeof($arr);$i++) {
        $line = explode("   ",$arr[$i]);
        $line[1] = mysqli_real_escape_string($linkID,$line[1]);
        $sql .= "insert into pdb_middle (hitid,description)
    VALUES ('$line[0]','$line[1]');";
        if($i %10 == 0) {
            if (mysqli_multi_query($linkID, $sql)) {
                echo "insert successfully!<br>";
                do {
                    if ($result = mysqli_store_result($linkID)) {
                        mysqli_free_result($result);
                    }
                }while(mysqli_next_result($linkID));
            } else {
                echo "Error: " . $sql . "<br>" . mysqli_error($linkID).'<br>';
            }
    
         $sql ="";
        }
    }
    if(mysqli_multi_query($linkID,$sql)){
        echo "insert successfully!";
    }else{
        echo "Error: ".$sql."<br>".mysqli_error($linkID);
    }
    
    mysqli_close($linkID);
    ?>
    

    相关文章

      网友评论

          本文标题:数据库批量插入操作

          本文链接:https://www.haomeiwen.com/subject/ykrtqxtx.html