MarioApprentice Posted January 30, 2013 Share Posted January 30, 2013 Hy everybody for the second time today. I'd like for my catch clause to redirect to a page regarding the mysql error given by a query. Lets say that i mispell a query and get a 1064 sql error code. how to i get it? I've tried googling but with no results. I've also found the errorCode() method that's going with PDO object and PDOStatement but it doesn't work. Quote Link to comment https://forums.phpfreaks.com/topic/273838-mysql-error-code/ Share on other sites More sharing options...
requinix Posted January 30, 2013 Share Posted January 30, 2013 Syntax errors are big. You should have found them during your development and testing. Advice: don't redirect. Recover from the error as best as you can (in code) and make sure it gets logged somewhere. Then you keep an eye on the logs. Quote Link to comment https://forums.phpfreaks.com/topic/273838-mysql-error-code/#findComment-1409211 Share on other sites More sharing options...
MarioApprentice Posted January 30, 2013 Author Share Posted January 30, 2013 I did come up on errors. That is why i'm asking but i didnt get that far ahead to now how to keep logs but i can imagine how to do it, but the main thing I want to do is get the sql error number. Let's say i get 1062 which is for duplicate code, or when i put the values in a table that are already there and need to be unique. Then i would like to do something like this: //registration of user with the unique values repetied, exception //sql error code 1062 if(1062 == true) { //redirect to registration page with a message that the username //or password or given e-mail already exists } That's what i want. I'm learning php for 2 months and I figure there's a better way to do this, but this is the best i can understand with a 2 months knowledge. I've tried with the errorCode() method in with the PDOStatement object but it doesn't work. Quote Link to comment https://forums.phpfreaks.com/topic/273838-mysql-error-code/#findComment-1409214 Share on other sites More sharing options...
requinix Posted January 30, 2013 Share Posted January 30, 2013 (edited) Don't rely on error conditions to tell you there's duplicate data. Actually look to see if there's a duplicate entry before trying to add a new one. [edit] If you're paranoid then there are a lot of different errors you can look for. PDOStatement::errorCode() will return something that is not the MySQL error number. Edited January 30, 2013 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/273838-mysql-error-code/#findComment-1409222 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.