13
votes

I am getting the following error message in CodeIgniter 2.1:

A PHP Error was encountered

Severity: Notice

Message: Array to string conversion

Filename: database/DB_active_rec.php

Line Number: 1407

I'm trying to update fields in my database. I have this code and every tutorial are just the same for batch upload. It works anyway but the thing is it display such an error.

this what i have in my model:

function update2($data){
   $this->db->update_batch('users',$data, "id");
}

This what i have in my controller:

public function updateValues(){
    $this->load->model('get_db');
    $newRow = array(
        array(
            'id' => '3',
            'firstname' => 'Rapphie'
        ),
        array(
            'id' => '2',
            'firstname' => 'Charmie'
        )
    );
    $this->get_db->update2($newRow);
    echo "it has been updated";
}
5
Which version of CodeIgniter are you running? You can see that with <?php echo CI_VERSION; ?> - P M
Dumb question, but if you do a print_r or var_export of ($data) just above the update_batch call in update2(), do you get the same array that you passed in? - King Skippus
the instruction you have written for update is wrong you should use a where condition see the documents codeigniter.com/user_guide/database/active_record.html#update - Muhammad Raheel

5 Answers

-2
votes

try this hope its work

function update2($data){
   foreach($data as $string_val){
   $this->db->update_batch('users',$string_val, "id");
    }
}

or

function update2($data){
   foreach($data as $string_val){
   $this->db->update_batch('users',$string_val->first_name, "id");
    }
}
47
votes

Just stumbled upon the exact same problem here. Fortunately, I'm using the same CI version. :)

It's true that the answer from M_A_K helps remove the "Notice", but I don't think that is the right solution for the problem. So I decided to take a look at line 1407 in DB_active_rec.php and I believe that this is nothing but a minor bug in CI 2.1.2.

Here's my fix. I simply changed the original code:

$not[] = $k.'-'.$v;

into this:

$not[] = $k2.'-'.$v2;

Voila! The "Notice" doesn't appear anymore. :)

We can clearly see that line 1407 was not meant to use $k and $v because line 1407 is inside a foreach loop iterating through $v as $k2 and $v2.

I hope I'm making myself clear.

2
votes

Just realized that using a foreach like M_A_K suggested actually beats the purpose of using the update_batch function. This is because by using foreach, we're actually doing single update (not batch) for each array elements. This is no different than using the (single) update function in CodeIgniter.

0
votes

I always feel a little dirty for doing this but you can always just suppress errors for that one line of code too!

function update2($data){
   @$this->db->update_batch('users',$data, "id");
}

Of course the proper thing to do is upgrade to 2.1.3 (haven't tested or looked so I'll assume it's fixed.)

0
votes

Correct solution is to remove lines 1401 - 1404 from DB_active_rec.php as the $not variable is not used anywhere.