Jump to content

mysql error code


MarioApprentice

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/273838-mysql-error-code/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/273838-mysql-error-code/#findComment-1409211
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/273838-mysql-error-code/#findComment-1409214
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/273838-mysql-error-code/#findComment-1409222
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.