当sql查询没有返回任何内容时,Codeigniter活动记录语句未运行

Mic*_*sby 7 php mysql activerecord codeigniter

我似乎遇到了以下代码的问题.当查询没有返回任何内容时,我无法获取插入查询.但是当db中有一行时,else语句会正确运行.有人知道为什么会发生这种情况吗?

$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);
}
Run Code Online (Sandbox Code Playgroud)

Phe*_*yne 7

您是否尝试将此更改if ($query5->num_rows() < 0) {为类似的内容if ($query5->num_rows() <= 0) {,只是一个想法.它会有所不同,因为你告诉它只有执行它less than zero然而它可能equal to zero会跳过else语句.

仅供CodeIgniter num_rows()参考:

/**
 * 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());
}
Run Code Online (Sandbox Code Playgroud)

  • 我得等几分钟 (2认同)

小智 7

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

if ($query5->num_rows() == 0)
Run Code Online (Sandbox Code Playgroud)