Welcome Guest, Not a member yet? Register   Sign In
Updating the same Table twice, even thought it should not
#1

[eluser]No0oB[/eluser]
I have been having a weird problem recently.
I have a function, which shall remove old entries from a database table (where saved timestamp is smaller than current timestamp)

Code:
function clearRecruitingQueue()
{
    $this->CI->db->select('`city_id`')->from(TBL_QUEUE_WORKER)->where('`worker_endStamp` <', time());
    $query = $this->CI->db->get();
    foreach ($query->result() as $row)
    {
        $this->CI->db->query('UPDATE '.TBL_CITY.' SET `city_worker` = `city_worker` + 1 WHERE `city_id` = '.$this->CI->db->escape($row->city_id));
        $this->CI->db->delete(TBL_QUEUE_WORKER, array('`city_id`', $row->city_id));
    }
}

However, when there is more than 1 item, it always updates one each time, the loop is run.
When I echo the city_id, it shows different IDs..

What is my mistake?
The queries do change, yet, it updates only one entry

Thanks in advance
#2

[eluser]tonanbarbarian[/eluser]
try the following
Code:
function clearRecruitingQueue()
{
    $this->CI->db->select('`city_id`')->from(TBL_QUEUE_WORKER)->where('`worker_endStamp` <', time());
    $query = $this->CI->db->get();
    $results = $query->result();
    $query->free_result();
    foreach ($results as $row)
    {
        $this->CI->db->simple_query('UPDATE '.TBL_CITY.' SET `city_worker` = `city_worker` + 1 WHERE `city_id` = '.$this->CI->db->escape($row->city_id));
        $this->CI->db->delete(TBL_QUEUE_WORKER, array('`city_id`', $row->city_id));
    }
}


the $query->result() inside the foreach may be a problem in PHP5 because all objects are created as references. The issue then is that db->query also returns a recordset like the earlier db->get does
There may have been problems whereby after the initial pass through the loop the $query->result() was actually returning the result from the update.

not sure if this really is the problem but it is worth a try




Theme © iAndrew 2016 - Forum software by © MyBB