How to retrieve database error from CodeIgniter |
[eluser]Erikk[/eluser]
Hello, How do you actually retrieve database error when inserting data for example ? db_debug would be turned off so you can handle errors your way - for example if you need so send back the error to an ajax request. What's the equivalent of say mysql_error() ? Thankx.
[eluser]Pascal Kriete[/eluser]
In a live app, you should never have to rely on errors to do your logic. You can always do checks to prevent them. Check if an entry exists, check if you're not getting any results. It's a much more reliable method. Errors are events you did not anticipate, not events to base decisions on.
[eluser]Erikk[/eluser]
The point is not in relying on errors to do the logic of the application, or checking if an entry exists etc.... Database errors are not necessarily related to the logic of your application or tables. An error can happen if the database is temporarily unavailable, or the connection to the database is broken or whatever, nothing to do with the logic of your application or your tables. An error can occur for example if someone changed the password etc. etc. Now if I use an ajax request that would return success if data could be retrieved in the database, and error along with the reason of the error if an error occurs (for example someone has turned down the database for maintenance or whatever), then I just can't use codeigniter database class to do that ![]()
[eluser]xwero[/eluser]
It's one of the weaknesses of the database class but it's also a hard to solve problem with the php4 compatibility. If the framework was php5 only you could add a try/catch blocks. If you use php5 and you really need it you could copy the driver you need give it a custom name and add try/catch blocks.
[eluser]drewsmiff[/eluser]
[quote author="inparo" date="1208201148"]In a live app, you should never have to rely on errors to do your logic. You can always do checks to prevent them. Check if an entry exists, check if you're not getting any results. It's a much more reliable method. Errors are events you did not anticipate, not events to base decisions on.[/quote] That may be true in PHP, but not in a lot of other languages. Checked vs. Unchecked Exceptions is a lengthy debate. What if he had a failover database and did something like: Code: try or what if he was logging for a db uptime metric when exceptions were caught? Code: try Errors ARE something you should anticipate...especially in a language like PHP where much of the logic is based of user interaction. Even in the 20 minute blog tutorial Derek Jones mentions that you should do boundary checking around num_rows() for all db calls. While that's not necessarily error handling, he's anticipating there not being any data, in which case, an error could occur. This is more about programming in general than CI or PHP per say.
[eluser]Erikk[/eluser]
I agree with drewsmiff. Errors must always be anticipated and handled in programs. In my opinion and according to my programming experience, what's most time consuming in writing a program is error handling: test for errors everywhere, decide how to handle them (logging, warning message ...). It's "easy" to write a software assuming that everything will work with no error.
[eluser]Pascal Kriete[/eluser]
I seem to have misunderstood your first post. Drewsmiff is right of course. I don't know how many times my java programs have tossed null pointer exceptions back at me. Those things are evil. So, to take another shot at your question. In most cases db errors will go to the exceptions class, so you can override that (see this bug). Another good practice with ajax requests is to have a timeout. Most of your users won't care what kind of error happened, so if the backend fails silently, you can toss them a 'server not responding' or something similar.
[eluser]VSz[/eluser]
If we truly use relational databases, than we should use constraints too. So if we can not just try to execute a delete query and check with an exception whether a constraint had stopped the deletion, there would be no use of relational databases. So we can not retrieve database errors from CI, and it means that we can not use any constraints?
[eluser]VSz[/eluser]
I mean, constraints, foreign keys, or even primary keys lose their purpose. |
Welcome Guest, Not a member yet? Register Sign In |