mohan.gade
mohan.gade

Reputation: 1105

How to know the number of rows deleted in mysql?

How to know the no of deleted rows in codeigniter provided that $this->db->affected_rows() returns every time ie. on success or on fail also

Upvotes: 3

Views: 13580

Answers (5)

Laurence
Laurence

Reputation: 60048

According to the Codeigniter userguide it returns the correct number of rows:

"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."

so therefore this should work:

 $this->db->delete('1', 'your_table');
 echo ($this->db->affected_rows()." rows were deleted");

If it doesn't work, then just do this as a non-ideal workaround

$count = $this->db->count_all('your_table');
$this->db->delete('1', 'your_table');
$new_count = $this->db->count_all('your_table');
if ($new_count > $count)
{
    echo (($new_count-$count)." rows was deleted");
}
else
{
    echo "nothing deleted";
}

Upvotes: 1

SoftwareCarpenter
SoftwareCarpenter

Reputation: 3923

$this->db->where($conditions)->delete('mytable');
return $this->db->affected_rows(); 

Upvotes: 5

YannPl
YannPl

Reputation: 1322

That would help if you were more specific, with example... But you can perform a SELECT COUNT(*) before deletion and after and compare the two results.

Upvotes: 0

Berry Langerak
Berry Langerak

Reputation: 18859

How to know the no of deleted rows in codeigniter provided that $this->db->affected_rows() returns every time ie. on success or on fail also

I don't use CodeIgniter, but normally, affected_rows( ) should return the number of rows affected. That means, if the query succeeded, it should always be an integer. If 0 is returned, no rows are deleted, if > 0, that number is deleted.

Upvotes: 3

Breezer
Breezer

Reputation: 10490

Have you tested it out, according to codeigniters website there is a hack that returns correct amount of rows, that's turned on by default.

http://codeigniter.com/user_guide/database/helpers.html

Upvotes: 1

Related Questions