Reputation: 1209
I have two values(bookingId) in index array and now i want to update query using these values but right now unable break array in loop, I want to run update query (according to count values in array) Here is my code,Where i am wrong ?
$bookingIds; // contaning two values 176,190
$counts = count($bookingIds); // counting array values
for ($x = 1; $x <= $counts; $x++)
{
$data = array('approved' => "1");
$this->db->where('bookingId', $bookingIds);
$this->db->update('notifications', $data);
}
Upvotes: 2
Views: 275
Reputation: 16373
Looks like you are using a framework; if it provides a whereIn()
method, it would be more efficient to use it (runs less SQL queries):
$data = ['approved' => '1'];
$this->db
->whereIn('bookingId', $bookingIds)
->update('notifications', $data);
(Just be aware you can't put thousands and thousands of values in such a "where in" clause, because the query would be huge. Personally I process this kind of stuff in chunks, like 500 records per query.)
(Also, make sure no error happens if the "where in" array (here, $bookingIds
) is empty. It's a special case, that the framework has to handle properly.)
Upvotes: 0
Reputation: 5507
One refactoring of the above using foreach to remove the whole indexing issue would be
$data = array('approved' => "1");
foreach ($bookingsIds as $bookingId )
{
$this->db->where('bookingId', $bookingId);
$this->db->update('notifications', $data);
}
Upvotes: 1
Reputation: 2584
I think you should mention an index for your array while looping.And your $x
must start with 0
$bookingIds; // contaning two values 176,190
$counts = count($bookingIds); // counting array values
for ($x = 0; $x < $counts; $x++)
{
$data = array('approved' => "1");
$this->db->where('bookingId', $bookingIds[$x]);
$this->db->update('notifications', $data);
}
Upvotes: 2