PDO Prepared Inserts multiple rows in single query

后端 未结 22 1947
感情败类
感情败类 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:46

    That's simply not the way you use prepared statements.

    It is perfectly okay to insert one row per query because you can execute one prepared statement multiple times with different parameters. In fact that is one of the greatest advantages as it allows you to insert you a great number of rows in an efficient, secure and comfortable manner.

    So it maybe possible to implement the scheme you proposing, at least for a fixed number of rows, but it is almost guaranteed that this is not really what you want.

    0 讨论(0)
  • 2020-11-21 23:47

    test.php

    <?php
    require_once('Database.php');
    
    $obj = new Database();
    $table = "test";
    
    $rows = array(
        array(
        'name' => 'balasubramani',
        'status' => 1
        ),
        array(
        'name' => 'balakumar',
        'status' => 1
        ),
        array(
        'name' => 'mani',
        'status' => 1
        )
    );
    
    var_dump($obj->insertMultiple($table,$rows));
    ?>
    

    Database.php

    <?php
    class Database 
    {
    
        /* Initializing Database Information */
    
        var $host = 'localhost';
        var $user = 'root';
        var $pass = '';
        var $database = "database";
        var $dbh;
    
        /* Connecting Datbase */
    
        public function __construct(){
            try {
                $this->dbh = new PDO('mysql:host='.$this->host.';dbname='.$this->database.'', $this->user, $this->pass);
                //print "Connected Successfully";
            } 
            catch (PDOException $e) {
                print "Error!: " . $e->getMessage() . "<br/>";
                die();
            }
        }
    /* Insert Multiple Rows in a table */
    
        public function insertMultiple($table,$rows){
    
            $this->dbh->beginTransaction(); // also helps speed up your inserts.
            $insert_values = array();
            foreach($rows as $d){
                $question_marks[] = '('  . $this->placeholders('?', sizeof($d)) . ')';
                $insert_values = array_merge($insert_values, array_values($d));
                $datafields = array_keys($d);
            }
    
            $sql = "INSERT INTO $table (" . implode(",", $datafields ) . ") VALUES " . implode(',', $question_marks);
    
            $stmt = $this->dbh->prepare ($sql);
            try {
                $stmt->execute($insert_values);
            } catch (PDOException $e){
                echo $e->getMessage();
            }
            return $this->dbh->commit();
        }
    
        /*  placeholders for prepared statements like (?,?,?)  */
    
        function placeholders($text, $count=0, $separator=","){
            $result = array();
            if($count > 0){
                for($x=0; $x<$count; $x++){
                    $result[] = $text;
                }
            }
    
            return implode($separator, $result);
        }
    
    }
    ?>
    
    0 讨论(0)
  • 2020-11-21 23:51

    Here's a class I wrote do multiple inserts with purge option:

    <?php
    
    /**
     * $pdo->beginTransaction();
     * $pmi = new PDOMultiLineInserter($pdo, "foo", array("a","b","c","e"), 10);
     * $pmi->insertRow($data);
     * ....
     * $pmi->insertRow($data);
     * $pmi->purgeRemainingInserts();
     * $pdo->commit();
     *
     */
    class PDOMultiLineInserter {
        private $_purgeAtCount;
        private $_bigInsertQuery, $_singleInsertQuery;
        private $_currentlyInsertingRows  = array();
        private $_currentlyInsertingCount = 0;
        private $_numberOfFields;
        private $_error;
        private $_insertCount = 0;
    
        function __construct(\PDO $pdo, $tableName, $fieldsAsArray, $bigInsertCount = 100) {
            $this->_numberOfFields = count($fieldsAsArray);
            $insertIntoPortion = "INSERT INTO `$tableName` (`".implode("`,`", $fieldsAsArray)."`) VALUES";
            $questionMarks  = " (?".str_repeat(",?", $this->_numberOfFields - 1).")";
    
            $this->_purgeAtCount = $bigInsertCount;
            $this->_bigInsertQuery    = $pdo->prepare($insertIntoPortion.$questionMarks.str_repeat(", ".$questionMarks, $bigInsertCount - 1));
            $this->_singleInsertQuery = $pdo->prepare($insertIntoPortion.$questionMarks);
        }
    
        function insertRow($rowData) {
            // @todo Compare speed
            // $this->_currentlyInsertingRows = array_merge($this->_currentlyInsertingRows, $rowData);
            foreach($rowData as $v) array_push($this->_currentlyInsertingRows, $v);
            //
            if (++$this->_currentlyInsertingCount == $this->_purgeAtCount) {
                if ($this->_bigInsertQuery->execute($this->_currentlyInsertingRows) === FALSE) {
                    $this->_error = "Failed to perform a multi-insert (after {$this->_insertCount} inserts), the following errors occurred:".implode('<br/>', $this->_bigInsertQuery->errorInfo());
                    return false;
                }
                $this->_insertCount++;
    
                $this->_currentlyInsertingCount = 0;
                $this->_currentlyInsertingRows = array();
            }
            return true;
        }
    
        function purgeRemainingInserts() {
            while ($this->_currentlyInsertingCount > 0) {
                $singleInsertData = array();
                // @todo Compare speed - http://www.evardsson.com/blog/2010/02/05/comparing-php-array_shift-to-array_pop/
                // for ($i = 0; $i < $this->_numberOfFields; $i++) $singleInsertData[] = array_pop($this->_currentlyInsertingRows); array_reverse($singleInsertData);
                for ($i = 0; $i < $this->_numberOfFields; $i++) array_unshift($singleInsertData, array_pop($this->_currentlyInsertingRows));
    
                if ($this->_singleInsertQuery->execute($singleInsertData) === FALSE) {
                    $this->_error = "Failed to perform a small-insert (whilst purging the remaining rows; the following errors occurred:".implode('<br/>', $this->_singleInsertQuery->errorInfo());
                    return false;
                }
                $this->_currentlyInsertingCount--;
            }
        }
    
        public function getError() {
            return $this->_error;
        }
    }
    
    0 讨论(0)
  • 2020-11-21 23:51

    Most of the solutions given here to create the prepared query are more complex that they need to be. Using PHP's built in functions you can easily creare the SQL statement without significant overhead.

    Given $records, an array of records where each record is itself an indexed array (in the form of field => value), the following function will insert the records into the given table $table, on a PDO connection $connection, using only a single prepared statement. Note that this is a PHP 5.6+ solution because of the use of argument unpacking in the call to array_push:

    private function import(PDO $connection, $table, array $records)
    {
        $fields = array_keys($records[0]);
        $placeHolders = substr(str_repeat(',?', count($fields)), 1);
        $values = [];
        foreach ($records as $record) {
            array_push($values, ...array_values($record));
        }
    
        $query = 'INSERT INTO ' . $table . ' (';
        $query .= implode(',', $fields);
        $query .= ') VALUES (';
        $query .= implode('),(', array_fill(0, count($records), $placeHolders));
        $query .= ')';
    
        $statement = $connection->prepare($query);
        $statement->execute($values);
    }
    
    0 讨论(0)
  • 2020-11-21 23:52

    A shorter answer: flatten the array of data ordered by columns then

    //$array = array( '1','2','3','4','5', '1','2','3','4','5');
    $arCount = count($array);
    $rCount = ($arCount  ? $arCount - 1 : 0);
    $criteria = sprintf("(?,?,?,?,?)%s", str_repeat(",(?,?,?,?,?)", $rCount));
    $sql = "INSERT INTO table(c1,c2,c3,c4,c5) VALUES$criteria";
    

    When inserting a 1,000 or so records you don't want to have to loop through every record to insert them when all you need is a count of the values.

    0 讨论(0)
  • 2020-11-21 23:52

    You can insert multiple rows in a single query with this function:

    function insertMultiple($query,$rows) {
        if (count($rows)>0) {
            $args = array_fill(0, count($rows[0]), '?');
    
            $params = array();
            foreach($rows as $row)
            {
                $values[] = "(".implode(',', $args).")";
                foreach($row as $value)
                {
                    $params[] = $value;
                }
            }
    
            $query = $query." VALUES ".implode(',', $values);
            $stmt = $PDO->prepare($query);
            $stmt->execute($params);
        }
    }
    

    $row is an array of arrays of values. In your case you would call the function with

    insertMultiple("INSERT INTO tbl (`key1`,`key2`)",array(array('r1v1','r1v2'),array('r2v1','r2v2')));
    

    This has the benefit that you use prepared statements, while inserting multiple rows with a single query. Security!

    0 讨论(0)
提交回复
热议问题