PDO Prepared Inserts multiple rows in single query

后端 未结 22 2099
感情败类
感情败类 2020-11-21 23:38

I am currently using this type of SQL on MySQL to insert multiple rows of values in one single query:

INSERT INTO `tbl` (`key1`,`key2`) VALUES (\'r1v1\',\'r1         


        
22条回答
  •  臣服心动
    2020-11-21 23:57

    I had the same problem and this is how i accomplish for myself, and i made a function for myself for it ( and you can use it if that helps you).

    Example:

    INSERT INTO countries (country, city) VALUES (Germany, Berlin), (France, Paris);

    $arr1 = Array("Germany", "Berlin");
    $arr2 = Array("France", "France");
    
    insertMultipleData("countries", Array($arr1, $arr2));
    
    
    // Inserting multiple data to the Database.
    public function insertMultipleData($table, $multi_params){
        try{
            $db = $this->connect();
    
            $beforeParams = "";
            $paramsStr = "";
            $valuesStr = "";
    
            for ($i=0; $i < count($multi_params); $i++) { 
    
                foreach ($multi_params[$i] as $j => $value) {                   
    
                    if ($i == 0) {
                        $beforeParams .=  " " . $j . ",";
                    }
    
                    $paramsStr .= " :"  . $j . "_" . $i .",";                                       
                }
    
                $paramsStr = substr_replace($paramsStr, "", -1);
                $valuesStr .=  "(" . $paramsStr . "),"; 
                $paramsStr = "";
            }
    
    
            $beforeParams = substr_replace($beforeParams, "", -1);
            $valuesStr = substr_replace($valuesStr, "", -1);
    
    
            $sql = "INSERT INTO " . $table . " (" . $beforeParams . ") VALUES " . $valuesStr . ";";
    
            $stmt = $db->prepare($sql);
    
    
            for ($i=0; $i < count($multi_params); $i++) { 
                foreach ($multi_params[$i] as $j => &$value) {
                    $stmt->bindParam(":" . $j . "_" . $i, $value);                                      
                }
            }
    
            $this->close($db);
            $stmt->execute();                       
    
            return true;
    
        }catch(PDOException $e){            
            return false;
        }
    
        return false;
    }
    
    // Making connection to the Database 
        public function connect(){
            $host = Constants::DB_HOST;
            $dbname = Constants::DB_NAME;
            $user = Constants::DB_USER;
            $pass = Constants::DB_PASS;
    
            $mysql_connect_str = 'mysql:host='. $host . ';dbname=' .$dbname;
    
            $dbConnection = new PDO($mysql_connect_str, $user, $pass);
            $dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
            return $dbConnection;
        }
    
        // Closing the connection
        public function close($db){
            $db = null;
        }
    

    If insertMultipleData($table, $multi_params) returns TRUE, your data has been inserted to your database.

提交回复
热议问题