1
votes

Alright, so i have a huge list (like 500+) of entries in an array that i need to insert into a MySQL database.

I have a loop that populates an array, like this:

$sms_to_insert[] = array(
    'text' => $text,
    'contact_id' => $contact_id,
    'pending' => $status,
    'date' => $date,
    'user_id' => $this->userId,
    'sent' => "1"
);

And then i send it to the database using the built insert_batch() function:

public function add_sms_for_user($id, $sms) {
    //$this->db->delete('sms', array("user_id" => $id)); Irrelevant
    $this->db->insert_batch('sms', $sms); // <- This!
}

The error message i get is as follows: Column count doesn't match value count at row 1.

Now, that doesn't make sense at all. The columns are the same as the keys in the array, and the values are the keys value. So, why is it not working?

Any ideas?

1
kindly mention the version of CIMoyed Ansari
Updated the system folder to the newest update, 2.1.0. Still having the same error. I'm gonna try to loop through the array and insert with insert() instead. Will get back with an update.qwerty
kindly review this document codeigniter.com/forums/viewthread/215142Moyed Ansari

1 Answers

2
votes

user_id turned out to be null in some situations, that's what caused the error.

EDIT: If you replace insert_batch() with a loop that runs insert() on the array keys you will get more clear error messages.