Welcome Guest, Not a member yet? Register   Sign In
$this->db->affected_rows() returns -1 after query
#1

[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) . "");
When I run
Code:
$this->db->affected_rows()  or $this->db->affected_rows($update)
it returns -1

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
#2

[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.
#3

[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().
#4

[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
#5

[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
#6

[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
#7

[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
#8

[eluser]bleu[/eluser]
Any Ideas as to how I can solve this issue
#9

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




Theme © iAndrew 2016 - Forum software by © MyBB