简体   繁体   中英

Codeigniter active record statement not running when sql query doesn't return anything

I seem to be having a problem with the below code. I cannot get the insert query to run when the query returns nothing. But when there is a row in the db, the else statement runs correctly. Does anybody know why this could be happening?

$query5 = $this->db->get_where('loggedstatus', array('userid_loggedstatus' => $userid));

if ($query5->num_rows() < 0) {
    $data1 = array('isLoggedIn_loggedstatus' => 'yes', 'userid_loggedstatus' => $userid);
    $this->db->insert('loggedstatus', $data1);
} else {
    $data = array('isLoggedIn_loggedstatus' => 'yes');
    $this->db->where('userid_loggedstatus', $userid);
    $this->db->update('loggedstatus', $data);
}

Have you tried changing this if ($query5->num_rows() < 0) { to something like if ($query5->num_rows() <= 0) { , just a thought. It would make a difference because your telling it to only execute if its less than zero however it might be equal to zero and you would skip to the else statement.

And just for CodeIgniter num_rows() reference:

/**
 * Number of rows in the result set
 *
 * @return  int
 */
public function num_rows()
{
    if (is_int($this->num_rows))
    {
        return $this->num_rows;
    }
    elseif (count($this->result_array) > 0)
    {
        return $this->num_rows = count($this->result_array);
    }
    elseif (count($this->result_object) > 0)
    {
        return $this->num_rows = count($this->result_object);
    }

    return $this->num_rows = count($this->result_array());
}

如果下面显示的条件改变应该解决问题。

if ($query5->num_rows() == 0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM