I have a project in codeigniter 2.0 and I used:
$query = $this->db->query('select * FROM my_table');
if($query->num_rows()==1) return true; else return false;
But in codeigniter 3.0.3 i don't know how to check (update, select, insert, delete... ).
for insert,update
$this->db->affected_rows();
Displays the number of affected rows, when
doing “write” type queries (insert, update, etc.).
if ( $this->db->affected_rows() > 0 )
{
return TRUE;
}
else
{
return FALSE;
}
for select
doing “select” type queries (insert, update, etc.).
if ($query->num_rows() > 0)
{
$dataSet = $query->result();
}
for delete
$res = $this->a_model->delete_product($id); // example delete
if($res == FALSE)
{
}else
{
}
$query = $this->db->query("SELECT * FROM my_table");
$dataSet = array();
if ($query->num_rows() > 0)
{
$dataSet = $query->result();
}
print_r($dataSet);
From the Cogidignitor User Guide 3:
For Select:
The number of rows returned by the query. Note: In this example, $query is the variable that the query result object is assigned to:
$query = $this->db->query('SELECT * FROM my_table');
if($query->num_rows() > 0){
return true;
}
else{
return false;
}
For UPDATE & INSERT Statements:
$this->db->affected_rows()
Displays the number of affected rows, when doing “write” type queries (insert, update, etc.).
Example:
$this->db->update('UPDATE statement');
$this->db->affected_rows();
$this->db->insert('INSERT statement');
$this->db->affected_rows();
Alternate for INSERT:
You can also use last insert id as like:
$this->db->insert('INSERT statement');
$id = $this->db->insert_id();
if($id > 0){
return true;
}
else{
return false;
}
For Delete:
DELETE always return the true in result if query executed else false, affected_rows will also return same:
$this->db->delete('DELETE statement');
if($this->db->affected_rows()){
return true;
}
else{
return false;
}
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.