PHP MySQLi Multiple Inserts

后端 未结 3 714
半阙折子戏
半阙折子戏 2020-12-02 01:13

I\'m wondering if prepared statements work the same as a normal mysql_query with multiple VALUES.

INSERT INTO table (a,b) VALUES (\'a\',\'b\'), (\'c\',\'d\')         


        
相关标签:
3条回答
  • 2020-12-02 01:43

    I went ahead and ran a test where one query uses a prepared statement, and the other builds the entire query then executes that. I'm probably not making what I'm wanting to know easy to understand.

    Here's my test code. I was thinking prepared statements sort of held back execution until a $stmt->close() was called to optimize it or something. That doesn't appear to be the case though as the test that builds the query using real_escape_string is at least 10 times faster.

    <?php
    
    $db = new mysqli('localhost', 'user', 'pass', 'test');
    
    $start = microtime(true);
    $a = 'a';
    $b = 'b';
    
    $sql = $db->prepare('INSERT INTO multi (a,b) VALUES(?, ?)');
    $sql->bind_param('ss', $a, $b);
    for($i = 0; $i < 10000; $i++)
    {
        $a = chr($i % 1);
        $b = chr($i % 2);
        $sql->execute();
    }
    $sql->close();
    
    echo microtime(true) - $start;
    
    $db->close();
    
    ?>
    
    0 讨论(0)
  • 2020-12-02 01:48

    If you use the prepared statement in a loop, it will be more efficient than running the raw query each time because of analysis that only needs to be done once with the prepared statement. So no, it is not the same, to that extent.

    0 讨论(0)
  • 2020-12-02 02:05
    public function insertMulti($table, $columns = array(), $records = array(), $safe = false) {
        self::$counter++;
        //Make sure the arrays aren't empty
        if (empty($columns) || empty($records)) {
            return false;
        }
    
        // If set safe to true: set records values to html real escape safe html
        if($safe === true){
            $records = $this->filter($records);
        }
    
        //Count the number of fields to ensure insertion statements do not exceed the same num
        $number_columns = count($columns);
    
        //Start a counter for the rows
        $added = 0;
    
        //Start the query
        $sql = "INSERT INTO " . $table;
    
        $fields = array();
        //Loop through the columns for insertion preparation
        foreach ($columns as $field) {
            $fields[] = '`' . $field . '`';
        }
        $fields = ' (' . implode(', ', $fields) . ')';
    
        //Loop through the records to insert
        $values = array();
        foreach ($records as $record) {
            //Only add a record if the values match the number of columns
            if (count($record) == $number_columns) {
                $values[] = '(\'' . implode('\', \'', array_values($record)) . '\')';
                $added++;
            }
        }
        $values = implode(', ', $values);
    
        $sql .= $fields . ' VALUES ' . $values;
        //echo $sql;
        $query = $this->dbConnection->query($sql);
    
        if ($this->dbConnection->error) {
            $this->errorLog($this->dbConnection->error, $sql);
            return false;
        } else {
            return $added;
        }
    }
    

    This function for first prepare the one INSERT query with multiple row values and Insert it once. But this is not for bulk insert at once.

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