Welcome Guest, Not a member yet? Register   Sign In
how to check if a row is update
#1

[eluser]runrun[/eluser]
Normally, I check updated row by doing this

Code:
if($this->db->affected_rows()>0){}

The problem is if the new updated data is the same as the existing data, the function will not return any thing, therefore can not decide if a row is updated or not.
#2

[eluser]cideveloper[/eluser]
Well in theory, If the new data is the same as the old data the table is NOT updated. If your concept is to check if the user "attempted" to update the table you might want to include a timestamps in your table with an "on update CURRENT_TIMESTAMP" attribute. Then you would get that value before the attempted update and then compare it when the attempted update is done.
#3

[eluser]Cristian Gilè[/eluser]
Or you can always select the field value to update and check if it's the same of the new value.


Cristian Gilè
#4

[eluser]lennierb5[/eluser]
I thought that if

Code:
$this->db->affected_rows() == 0 // Update query was ran successfully but nothing was updated
$this->db->affected_rows() == 1 // Update query was ran successfully and 1 row was updated
$this->db->affected_rows() == -1 // Query failed

So you should be able to check for 1 if you want to know if anything has been updated.
#5

[eluser]Cristian Gilè[/eluser]
[quote author="lennierb5" date="1295148732"]I thought that if

Code:
$this->db->affected_rows() == 0 // Update query was ran successfully but nothing was updated
$this->db->affected_rows() == 1 // Update query was ran successfully and 1 row was updated
$this->db->affected_rows() == -1 // Query failed

So you should be able to check for 1 if you want to know if anything has been updated.[/quote]
Your assumption is not true. Here, an example:
Code:
$this->db->where('id',27);
$this->db->update('my_table',$some_data);

If id=27 doesn't exist the query doesn't fail and the return value for affected_rows is 0.


Cristian Gilè
#6

[eluser]runrun[/eluser]
progr@mmer's solution is great.




Theme © iAndrew 2016 - Forum software by © MyBB