![]() |
$this->db->affected_rows() returns -1 after query - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: $this->db->affected_rows() returns -1 after query (/showthread.php?tid=51469) |
$this->db->affected_rows() returns -1 after query - El Forum - 05-05-2012 [eluser]bleu[/eluser] I have a query like this Code: $update= $this->db->query("update aas set aa = 'aa' where no=" . $this->db->escape($No) . ""); Code: $this->db->affected_rows() or $this->db->affected_rows($update) When updating any row which exists I get -1 Even when I have no row to update it still shows -1. What is the issue? I am using codeigniter 2.1.0 with mysqli drivers $this->db->affected_rows() returns -1 after query - El Forum - 05-07-2012 [eluser]Aken[/eluser] Is the query you're generating correct? You can check using $this->db->last_query(). Are you running the affected_rows() method IMMEDIATELY after performing the query? If not, other DB manipulations may be altering the expected results. $this->db->affected_rows() returns -1 after query - El Forum - 05-07-2012 [eluser]CroNiX[/eluser] Also, if you are updating, affected_rows() will only return a positive number if the data actually changes. If it doesn't change, the update doesn't actually take place even thought the query is "successful". So if the data was: id:21 name:Bill and you do an update setting the name to 'Bill' for id 21, no update will take place. One thing you aren't checking, is the result of $update to see whether there was an error or not. It will return TRUE if the query was successfully run and FALSE if there was an error. Check for an error first before checking to see if the update had affected_rows(). $this->db->affected_rows() returns -1 after query - El Forum - 05-08-2012 [eluser]bleu[/eluser] [quote author="Aken" date="1336375382"]Is the query you're generating correct? You can check using $this->db->last_query(). Are you running the affected_rows() method IMMEDIATELY after performing the query? If not, other DB manipulations may be altering the expected results.[/quote] Yes the query I am generating correct($this->db->last_query() returns the same query I run manually in phpmyadmin) I have tried running it in phpmyadmin and it gives me the proper 0 or 1 rows affected as per the data.But when i run it through codeigniter I get -1 Is it because I have turned on codeigniter mysqli drivers $this->db->affected_rows() returns -1 after query - El Forum - 05-08-2012 [eluser]bleu[/eluser] [quote author="CroNiX" date="1336412575"]Also, if you are updating, affected_rows() will only return a positive number if the data actually changes. If it doesn't change, the update doesn't actually take place even thought the query is "successful". So if the data was: id:21 name:Bill and you do an update setting the name to 'Bill' for id 21, no update will take place. One thing you aren't checking, is the result of $update to see whether there was an error or not. It will return TRUE if the query was successfully run and FALSE if there was an error. Check for an error first before checking to see if the update had affected_rows().[/quote] I have tried running it in phpmyadmin and it gives me the proper 0 or 1 rows affected as per the data.But when i run it through codeigniter I get -1 even when The value to be updated has changed or remained same The query is true always in codeigniter Is it because I have turned on codeigniter mysqli drivers $this->db->affected_rows() returns -1 after query - El Forum - 05-08-2012 [eluser]InsiteFX[/eluser] 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. Maybe it was not updated for use with MySQLI $this->db->affected_rows() returns -1 after query - El Forum - 05-09-2012 [eluser]bleu[/eluser] [quote author="InsiteFX" date="1336485458"]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. Maybe it was not updated for use with MySQLI [/quote] If it is not updated for MySQLI, how will I get to know whether my query has updated a row or not $this->db->affected_rows() returns -1 after query - El Forum - 05-10-2012 [eluser]bleu[/eluser] Any Ideas as to how I can solve this issue $this->db->affected_rows() returns -1 after query - El Forum - 11-17-2013 [eluser]Needle[/eluser] Was there ever a resolution for this? I am having the same issue, using CI 2.1.4 with MySQLi drivers. The DELETE affected rows hack is enabled, but it always returns "0" even on success. |