Welcome Guest, Not a member yet? Register   Sign In
$query->affected_rows() (Active Record) ?
#1

[eluser]Sayian[/eluser]
I've looked over the manual trying to find a way to test if an insert or update statement actually went through using active record.

I cannot find material related to this question.

I'm running on zero sleep and too much coffee so if I over looked it don't shoot me Smile

Thanks in advance for the assistance.
#2

[eluser]Armchair Samurai[/eluser]
Try $this->db->affected_rows().

http://ellislab.com/codeigniter/user-gui...lpers.html
#3

[eluser]Sayian[/eluser]
Cheers to you sir.
#4

[eluser]squarebones[/eluser]
Cavaet: affected_rows() returns > 0 ONLY if a change is made to the fields in question after an UPDATE query. If no changes are made (the same data is being written back to the table entry) then the affected_rows() returns 0.

Now, is there some way to detect that the query was successful? Not that I know of. If the update occurs and there is an error, the resultant problem will be handled by the database error routine which throws that beautiful red box onto screen saying there was a database error (sorry about the redundancy).

How do we catch that error and handle it before the user sees that ugly message that halts all operations?

This affects MySQL (though it may not affect other database types).
#5

[eluser]krif[/eluser]
just wanted to bump this. Same problem here. affected_rows() returns 0 if you run an update() that writes back exactly the same data that existed before. Makes sense, but like squarebones, I'd like to catch a db error.
#6

[eluser]JanDoToDo[/eluser]
I'd also like to know how to check if a query executed and handle it.
#7

[eluser]JHackamack[/eluser]
@JanDoToDo

Code:
if($this->db->update('table',$data)) {
//SUCCESS
} else {
//FAILURE
}
#8

[eluser]JanDoToDo[/eluser]
awesome thankyou. Smile Also, what are the errors returned as?? Can I cycle through them as an array?
#9

[eluser]JHackamack[/eluser]
[quote author="JanDoToDo" date="1264047743"]awesome thankyou. Smile Also, what are the errors returned as?? Can I cycle through them as an array?[/quote]

Looking Here:
http://ellislab.com/codeigniter/user-gui...tions.html

It looks like you can specify if you want to show errors, but there's no way to get them out of CI, unless I overlooked something.




Theme © iAndrew 2016 - Forum software by © MyBB