0

I checked around to see what exactly affected_rows returns. It's supposed to return > 0 if something was deleted, and 0 if nothing was, correct?

But when I delete a product, it is deleted because it exists via the product id. But when I want to test whether the product in question has been deleted by trying to do this in my model:

function delete_product($id)
{
    $tables = array('products', 'attributes');
    $this->db->where('p_id', $id);
    $this->db->delete($tables);


    if ($this->db->affected_rows() > 0)
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}

and returning the value to my controller as such:

public function delete()
{
    $id = $this->uri->segment(3);

    $this->a_model->delete_product($id);

    if($res == FALSE)
    {
        $this->session->set_flashdata('success_delete', 'Product deleted successfully.');
        redirect('admin/index');
    }
    else
    {
        $this->session->set_flashdata('error_delete', 'Product not deleted. We gots an issue.');
        redirect('admin/index');
    }
}

The returned value is always false i.e. 0. But when I check my database to see whether the product was deleted, it is deleted. Could someone point out what I'm doing wrong?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
a7omiton
  • 1,597
  • 4
  • 34
  • 61

6 Answers6

2

affected_rows() only applies to "write" queries.

Displays the number of affected rows, when doing "write" type queries (insert, update, etc.).

Note: In MySQL "DELETE FROM TABLE" returns 0 affected rows. The database class has a small hack that allows it to return the correct number of affected rows. By default this hack is enabled but it can be turned off in the database driver file.

You might want to make sure the hack is enabled. http://ellislab.com/codeigniter/user-guide/database/helpers.html

Community
  • 1
  • 1
Trenton Trama
  • 4,890
  • 1
  • 22
  • 27
1
$this->a_model->delete_product($id);

if($res == FALSE)

should be:

$res = $this->a_model->delete_product($id);

if ($res === FALSE)

You are not assigning a value to $res and you are not assigning a variable to the value of $this->a_model->delete_product($id). You also want to use === to make a strict comparison when dealing boolean just to be safe as best practice.

You can always just do this too:

if (!$this->a_model->delete_product($id))
kittycat
  • 14,983
  • 9
  • 55
  • 80
  • This works as well, but I've decided to go with transactions, as I believe it won't commit to the delete if for some reason the query ends up being false – a7omiton Mar 11 '13 at 22:48
1

Try this:

 if($this->db->delete($tableName))
    return true;
     else
        return false;
Rahul Chipad
  • 2,373
  • 17
  • 20
1

The post is old but I had the same problem. Since the post is not "solved" and still pertinent, I add my solution.

Actually, delete return a boolean directly so the function delete_product can be simplified like :

function delete_product($id)
{
  $tables = array('products', 'attributes');
  $this->db->where('p_id', $id);
  $res = $this->db->delete($tables);
  return $res;
}
Eiji
  • 454
  • 6
  • 15
  • Be aware that, the returned boolean value simply means "query ran successfully", and a `TRUE` value does not necessarily means there are rows that were actually affected (in case of no records matching the where condition). You should still check `affected_rows()` to determine if any rows were in fact deleted. – Mu-Tsun Tsai Jun 03 '20 at 03:55
1
function delete_product($id){
  $this->db->trans_start();
    $tables = array('products', 'attributes');
    $this->db->where('p_id', $id);
    $this->db->delete($tables);
  $this->db->trans_complete();

  if($this->db->trans_status() === FALSE){
    return false;
  }else{
    return true;
  }
}
-1

There is some mistakes

You have write : if ($this->db->affected_rows() > 0)

but it should be : if ($this->db->affected_rows > 0)

you need to remove () from affected_rows() and write affected_rows only...

mikdiet
  • 9,859
  • 8
  • 59
  • 68