Codeigniter active write statement does not work when SQL query returns nothing

I seem to have a problem with the code below. I cannot get the insert request to start when the request returns nothing. But when there is a line in db, the else statement executes correctly. Does anyone know why this might happen?

$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); } 
+7
source share
2 answers

You tried to change this if ($query5->num_rows() < 0) { to something like if ($query5->num_rows() <= 0) { , just a thought. This will make a difference because you tell him that it will only execute if it is less than zero , but can be equal to zero , and you will go to the else statement.

And only for CodeIgniter num_rows() help:

 /** * 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()); } 
+7
source

Change if the condition shown below should fix the problem.

 if ($query5->num_rows() == 0) 
+7
source

All Articles