INSERT IGNORE using Codeigniter

后端 未结 13 977
眼角桃花
眼角桃花 2020-12-08 19:52

I am trying to insert a few rows into the MySQL table using Codeigniter and Active Records.

PHP Code

$data = array(\'......\');  //          


        
13条回答
  •  醉话见心
    2020-12-08 20:36

    add to file DB_query_builder.php

    this 2 functions

    public function insert_ignore_batch($table = '', $set = NULL, $escape = NULL) {
        if ($set !== NULL)
        {
            $this->set_insert_batch($set, '', $escape);
        }
    
        if (count($this->qb_set) === 0)
        {
            // No valid data array. Folds in cases where keys and values did not match up
            return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE;
        }
    
        if ($table === '')
        {
            if ( ! isset($this->qb_from[0]))
            {
                return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE;
            }
    
            $table = $this->qb_from[0];
        }
    
        // Batch this baby
        $affected_rows = 0;
        for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100)
        {
            $this->query($this->_insert_ignore_batch($this->protect_identifiers($table, TRUE, $escape, FALSE), $this->qb_keys, array_slice($this->qb_set, $i, 100)));
            $affected_rows += $this->affected_rows();
        }
    
        $this->_reset_write();
        return $affected_rows;
    }
    
    protected function _insert_ignore_batch($table, $keys, $values) {
        return 'INSERT IGNORE INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
    }
    

    using:

    $this->db->insert_ignore_batch('TableName', $data);
    

提交回复
热议问题