CodeIgniter Forums
insert_batch returning -1 when query fails - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Development (https://forum.codeigniter.com/forumdisplay.php?fid=6)
+--- Forum: CodeIgniter 3.x (https://forum.codeigniter.com/forumdisplay.php?fid=17)
+--- Thread: insert_batch returning -1 when query fails (/showthread.php?tid=66773)



insert_batch returning -1 when query fails - sneakyimp - 11-30-2016

I'm trying to work with the query builder's insert_batch function. According to the documentation, it should return FALSE on failure. That is not the case.

Try this controller method:
PHP Code:
    public function barf() {
        
$data = array(
                array(
                        
'title' => 'My title',
                        
'name' => 'My Name',
                        
'date' => 'My date'
                
),
                array(
                        
'title' => 'Another title',
                        
'name' => 'Another Name',
                        
'date' => 'Another date'
                
)
        );
        
$retval $this->db->insert_batch("i_do_not_exist"$data);
        
var_dump($retval);
    } 

Seems to me it should probably throw an exception and at the very least it should return false.


RE: insert_batch returning -1 when query fails - sneakyimp - 11-30-2016

Note insert_batch will also return -1 if you attempt to insert a record with a duplicate primary key value.


RE: insert_batch returning -1 when query fails - sneakyimp - 11-30-2016

Also, the documentation on update_batch is incorrect:
http://www.codeigniter.com/userguide3/database/query_builder.html?highlight=update_batch#CI_DB_query_builder::set_update_batch
It lists the third param as this:
Code:
$value (string) – Field value, if $set is a single field
In the code, it's different:
Code:
    /**
     * Update_Batch
     *
     * Compiles an update string and runs the query
     *
     * @param    string    the table to retrieve the results from
     * @param    array    an associative array of update values
     * @param    string    the where key
     * @return    int    number of rows affected or FALSE on failure
     */
    public function update_batch($table, $set = NULL, $index = NULL, $batch_size = 100)
    {
        // blah blah blah
    {



RE: insert_batch returning -1 when query fails - Narf - 12-01-2016

Do you have to post this on 3 different places?


RE: insert_batch returning -1 when query fails - sneakyimp - 12-02-2016

I really apologize. I was trying to implement something with it and was casting around for help. Kinda had my head up my rear.