CodeIgniter Forums
$this->db->affected_rows() doesnt return no. rows deleted??? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: $this->db->affected_rows() doesnt return no. rows deleted??? (/thread-2480.html)



$this->db->affected_rows() doesnt return no. rows deleted??? - El Forum - 08-07-2007

[eluser]the real rlee[/eluser]
Hi guys, i was reading the user guide and supposedly an inbuilt hack in the Active Record class for mysql should make $this->db->affected_rows() return the actual rows deleted...But i cant seem to get it to do so. My sql definitely deletes the record

Code:
$this->db->delete('gallery_photo', array('gallery_photo_id' => (int)$gallery_photo_id));
        $affected_rows = $this->db->affected_rows();
        if ($affected_rows == 0) {
            show_error('Gallery Model: Invalid photo ID ('.$gallery_photo_id.') unable to delete photo (affected rows: '.$this->db->affected_rows().')');
            log_message('error', 'Gallery Model: Invalid photo ID ('.$gallery_photo_id.') unable to delete photo.');
        }

Am i missing something?


$this->db->affected_rows() doesnt return no. rows deleted??? - El Forum - 08-09-2007

[eluser]the real rlee[/eluser]
Hate to bump this, but i really would like a solution lol.


$this->db->affected_rows() doesnt return no. rows deleted??? - El Forum - 08-09-2007

[eluser]Derek Allard[/eluser]
If you echo out $affected_rows without your if statement, what do you see?

What might be happening is that you are removing a single row, thus CI is returning "1" that is evaluating to true? Thus, your code is really

if (true == false)


$this->db->affected_rows() doesnt return no. rows deleted??? - El Forum - 08-10-2007

[eluser]Michael Wales[/eluser]
With his code, if it was deleting a single row - his code would still not execute the logging and such.
Code:
if ($affected_rows == 0) { // if (true == false)
  // This is never run, because true != false
}

I agree, echoing out $affected_rows is the first step in solving this issue though. Does $affected_rows in fact = 0? What if affected_row = FALSE and it's a completely seperate issue?


$this->db->affected_rows() doesnt return no. rows deleted??? - El Forum - 08-10-2007

[eluser]Derek Allard[/eluser]
[quote author="walesmd" date="1186777143"]With his code, if it was deleting a single row - his code would still not execute the logging and such.[/quote]

Yeah, that was the point I was trying (inarticulately) to make Wink
Quote:I agree, echoing out $affected_rows is the first step in solving this issue though. Does $affected_rows in fact = 0? What if affected_row = FALSE and it's a completely seperate issue?

You bet! Let us know what your echo reveals.