Dealing with Errors in WordPress

Whenever you works with queries, if the query has any mistake. If a custom query fails or error, and nothing is returned so it’s hard to diagnose what is wrong with your query. For displaying MySQL errors to the page the wpdb class provides functions. Here’s an example of using these functions:



$liveposts = $wpdb->get_results( $wpdb->prepare(“SELECT post_title, ID FROM $wpdb->posts_webdirectors WHERE post_status = ‘publish'”) );



Before you execute a query the show_errors() function must be called directly. When you executing a query then the print_error() function must be called directly. The error messages are displayed, If there are any errors in your SQL statement or query. For hide all MySQL errors you can also called the $wpdb->hide_errors() function,or call the $wpdb->flush() function to delete the cached query results. In wordpress the database class contains additional variables for store information about WordPress queries. Below is a list of some of the more common variables which use:

var_dump( $wpdb->num_queries ); // total number of queries run

var_dump( $wpdb->num_rows ); // total number of rows returned by the last query

var_dump( $wpdb->last_result ); // most recent query results

var_dump( $wpdb->last_query ); // most recent query executed

var_dump( $wpdb->col_info ); // column information for the most recent query

For see the result of query add the preceding code directly after you execute. This code is very help full that why a database query isn’t working as you want.

 The $queries variable is another very powerful database variable. It stores all of the queries run by WordPress. For enable this variable, In wp-config.php file contain a constant variable with the name of SAVEQUERIES you must do it first true then it will be work. On each page which query is executing this variable tells and it store in the $queries variable. First find this wp-config.php file and then do it true as shown: define( ‘SAVEQUERIES’, true ); All the queries now stored in the $queries variable. All of the query information You can display like that:

var_dump( $wpdb->queries ); // displays all queries executed during page load

When troubleshooting slow load times this is especially handle. An indecent number of queries executing by a plugin, so here will be dramatically slow down load times in WordPress. For disable the SAVEQUERIES constant option when you have finished to viewing the queries it can also slow down load times because it storing all queries.

In the WordPress when when working with the database directly then the database query class is a major asset, When you develop a plugin or making a complex loop as you will see. Which you have learned in the previously mentioned database class functions how to use specific escaping techniques to verify that your queries are executed in the safest manner possible or not. Any user don’t want a handcrafting an input item that contains DROP TABLES as a bad SQL injection, you may loss of your WordPress database tables.

Escaping functions and the query preparation that inputs don’t become SQL functions. It is very important that you follow these methods and function for querying data and to ensure that your website is the most efficient and uses the safest techniques possible that you have used.