Possible PDOException Errors (MySQL 5)?

By : Wilco
Source: Stackoverflow.com

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.

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

Edit: 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.

By : Wilco


The MySQL documentation is the complete reference for error codes.

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

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

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

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

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

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

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.

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

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() 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.

I use UDP a lot, and the main "reliability" issue I end up seeing is lost packets (particularly in fragmented IP packets). You could probably do 90% of what needs to be done by preventing fragmenting (add a layer that chops up datagrams into IP-sized chunks), and by having the recipeint detect and request resends for lost "chunks".

However, this kind of thing is really what TCP was invented for. UDP is best used for time-sensitive data that would be stale on a resend anyway.

There is a network topological solution. Just arrange things so that there can't be any collisions (the #1 source of lost packets on a LAN). Here's what we do where I work to make UDP reliable:

  • Put one client and server on a dedicated ethernet link (perhaps a switch between them, but no other systems on their private LAN).
  • Keep a strict client-server communications protocol on the UDP LAN. The server is never allowed to talk, except in response to the client.
  • Turn off all exteranious networking garbage on that UDP link (Netbios, etc).
  • Make ARP entries on both ends static (so ARP won't interfere once every 10 minutes).

(Note: The last one is particularly important. On many systems IP packets that cause an ARP request are just thrown away, rather than sent after ARP is resolved).

By : T.E.D.

You need to think about what reliable means. Also you need to decide if you need the packets in order or if out of order is ok. If out of order is ok you need to come up with an ACK, re-transmit, and timeout scheme. Also you need to decide if you are going to handle packet fragmentation. If you can get away with it yo might want to limit the size of a packet to prevent fragmentation.

By : Rex Logan

This video can help you solving your question :)
By: admin