可能的PDOException错误(MySQL 5)? [英] Possible PDOException Errors (MySQL 5)?

查看:283
本文介绍了可能的PDOException错误(MySQL 5)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我为我的网络应用程序设置安装程序,并有数据库凭据的输入字段。我的验证过程的一部分包括测试数据库连接(使用PHP的PDO库)。如果连接失败,我想能够区分错误的密码,坏地址,不存在的数据库名称等,所以我可以参考在表单上正确的输入字段。

So I'm setting up an installer for my web app, and have input fields for database credentials. Part of my validation process includes testing the database connection (using PHP's PDO library). If the connection fails, I want to be able to differentiate between a bad password, bad address, nonexistent database name, etc. so I can reference the proper input field on the form.

一个人可以指向一个参考,概述使用PDOException返回的可能的错误代码/消息。

Can anone point me towards a reference that outlines the possible error codes/messages that are returned with a PDOException?

编辑:我发现这些错误代码/消息可能是数据库特定的,本机数据库代码/错误可能只是通过。如果是这样,我目前只使用MySQL 5数据库。

It occurred to me that these error codes/messages are probably database-specific and the native database codes/errors may simply be getting passed through. If this is the case, I am currently only working with MySQL 5 databases.

推荐答案

MySQL文档是< a href =http://dev.mysql.com/doc/refman/5.1/en/error-handling.html =noreferrer>错误代码。

从1000开始的错误代码是服务器错误。这些错误包括:

Error codes starting at 1000 are server errors. These include errors like:


  • 错误:1045 SQLSTATE:28000( ER_ACCESS_DENIED_ERROR
    消息:用户'%s'@'%s'拒绝访问(使用密码:%s)

  • Error: 1045 SQLSTATE: 28000 (ER_ACCESS_DENIED_ERROR) Message: Access denied for user '%s'@'%s' (using password: %s)

错误:1049 SQLSTATE :42000( ER_BAD_DB_ERROR
消息:未知数据库'%s'

Error: 1049 SQLSTATE: 42000 (ER_BAD_DB_ERROR) Message: Unknown database '%s'

从2000开始的错误代码为客户端错误。这些错误包括:

Error codes starting at 2000 are client errors. These include errors like:


  • 错误:2005( CR_UNKNOWN_HOST ) :未知的MySQL伺服器主机'%s'(%d)

  • Error: 2005 (CR_UNKNOWN_HOST) Message: Unknown MySQL server host '%s' (%d)

错误:2003( CR_CONN_HOST_ERROR )消息:无法连接到%s上的MySQL服务器(%d)

Error: 2003 (CR_CONN_HOST_ERROR) Message: Can't connect to MySQL server on '%s' (%d)

列出所有可能的错误,因为它们已经记录在案,我不知道你需要处理哪些。例如,错误2001和2002特定于UNIX套接字连接,这可能与您的目标平台无关。

I'm not going to list all possible errors, because they're already documented, and I don't know which ones you need to handle. For instance, errors 2001 and 2002 are specific to UNIX socket connections, which may be irrelevant to your target platform.

不要忘记使用 PDO :: errorCode() PDO :: errorInfo() 简单的 PDOException 消息。

Don't forget to use PDO::errorCode() and PDO::errorInfo() instead of simply the PDOException message.

code> getCode() - 不,似乎不支持这种方式。我做了一个快速测试, var_dump() a PDOException 。不幸的是,即使错误代码和SQLSTATE包含在异常消息中,它的代码也是简单的0。

Re your comment about getCode() -- No, it doesn't seem to be supported in that way. I did a quick test, to var_dump() a PDOException. Unfortunately, its code is simple "0" even though the error code and SQLSTATE are included in the exception message.

Exception :: getCode()是基础 Exception 类的一部分, PHP版本5.1.0。这是由相应的PDO驱动程序实现来利用这个对象字段。至少对于MySQL驱动程序,他们显然没有。

Exception::getCode() is part of the base Exception class, as of PHP version 5.1.0. It's up to the respective PDO driver implementation to utilize this object field. At least for the MySQL driver, they apparently didn't.

这篇关于可能的PDOException错误(MySQL 5)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆