Feature Request: Return error numbers in WPDB
I have recently started developing plugins with WordPress, previously not using any CMS – rather raw PHP. One of the first things I noticed was the lack of ability to get error strings or error numbers returned to me with wpdb.
Here is my situation:
I have created a table for my plugin. A user may input data to be stored in the table, however one of the fields contains a unique constraint. As you may understand, I wish to handle unique constraint errors differently from other errors, simply returning ‘x’ already exists in this table and handling other errors alternatively.
Currently I use PHP’s output buffering to get the error, load it into a string and delete it from the buffer before it is output – this allows me to search for the term ‘duplicate entry’ in the error to handle this.
ob_start(); $wpdb->show_errors(); $wpdb->print_error(); $wpdb->hide_errors(); $error = ob_get_contents(); ob_end_clean();
So this works, but is quite hackish and not preferable, I can imagine it takes quite a performance hit. In my opinion there is no reason this should not be implemented, as it is not hard at all to implement, and will not cause incompatability issues.
If anyone knows of any alternative to handling this, I am interested to know.
- The topic ‘Feature Request: Return error numbers in WPDB’ is closed to new replies.