RED
RED

Reputation: 57

how to get the error message when query is update or not codeigniter

how to get the error message when query is update or not codeigniter

I am using codeigniter library please help
I don't care about the affect row's

$ci = &get_instance();
$ci->db->where('activity_id',$activity_id);
$ci->db->update('activity',$activity_data);

if(error){
    return false / or error message ;
}else{
    return true;
}

Upvotes: 0

Views: 2423

Answers (3)

NomanJaved
NomanJaved

Reputation: 1390

You can simply know, whether your data is entered in database or not.
Model

$row = $this->db->affected_rows()
if($row ){
   return true;
}else{
   return false; 
}

Then in controller check condition and then you can set the flash data and show in the view.

$this->session->set_flashdata('err_message', 'Data not updated successfully.');

remember that you need to load the session library before use it.

View

<?php
  if($this->session->flashdata('err_message')){
?>
<div class="alert alert-danger">
  <?php echo $this->session->flashdata('err_message'); ?>
</div>

and you can then return the $row that may be have value or empty. Hope this will help you.

Upvotes: 1

Sidiq Aldi
Sidiq Aldi

Reputation: 16

You can add it into variable first

try this

$result = $ci->db->update('activity',$activity_data);
if($result){
   return true;
}else{
   return $this->db->_error_message(); 
}

Upvotes: 0

Rafael Mota
Rafael Mota

Reputation: 127

try this

if (!(bool)$ci->db->where('activity_id',$activity_id)->update('activity',$activity_data)) 
{
//your error
}

Upvotes: 0

Related Questions