DEV Community

dirty_pig
dirty_pig

Posted on

Methods for Viewing SQL in WordPress

After years of experience in WordPress development, our editor has compiled three methods for viewing SQL queries executed in WordPress. We hope one of them suits your needs.

Method One: Using WordPress Plugins

The easiest method is to directly install a WordPress plugin, such as Query Monitor.

Query Monitor

Query Monitor Example

Editor's Review:

Query Monitor is similar to the debugging tools built into ThinkPHP and Laravel, making it a decent choice for occasional SQL query viewing. However, when dealing with pages in WordPress that contain dozens or even hundreds of SQL queries, using this tool may seem less responsible.

Drawback: When developing APIs or encountering code errors, Query Monitor may not display relevant information. While it is functional, it is not an ideal choice and can be compared to someone unreliable, always around during routine times but disappearing when needed most. Moreover, its menu is not very user-friendly and requires multiple clicks to access.

Recommended Rating: 1 star (out of 5)

Method Two: Logging and Printing SQL Queries in WordPress Code

You can enable SQL query logging by editing the wp-config.php file located in the WordPress root directory.

Add the following code to the file:

define('SAVEQUERIES', true);
Enter fullscreen mode Exit fullscreen mode

Then, in the place where you want to view SQL queries, add the following code:

<?php
global $wpdb;
var_dump($wpdb->queries);
exit;
?>
Enter fullscreen mode Exit fullscreen mode

This will enable WordPress to log every executed SQL query, including their execution times, and store them in a global array named queries.

Editor's Review:

Imagine having to find the right place to add code every time you need to view SQL queries, then removing the code after checking, and potentially re-adding it if the problem remains unresolved. This repetitive process can be quite exhausting.

Drawback: It requires code modification, which needs to be repeated daily. Even if you define these few lines of code as macros to avoid manual input, it can still be mentally draining. Fatigue may lead to the risk of accidentally submitting erroneous code to the production environment.

Recommended Rating: 2 stars (out of 5)

Method Three: Using External Tools

1. Enabling MySQL Query Logging

You can make MySQL record all executed SQL queries by simply adding the following code to the my.cnf file in MySQL:

# Log SQL queries
general_log = 1
general_log_file = /var/log/mysql/general_sql.log
Enter fullscreen mode Exit fullscreen mode

sql view
Editor's Review:

To use this method, you need permissions on the MySQL server. If multiple people share a MySQL development server, it's best not to enable this feature as it can cause confusion. Additionally, due to the extensive information in the logs, it's challenging to identify which queries are yours. Therefore, it is recommended for use on a test server and only enabled when crucial issues need to be resolved.

Drawback: You need to log in to the MySQL server to view the logs. If MySQL encounters issues, it may raise suspicion. Moreover, since it logs all SQL queries, finding the queries relevant to you can be challenging due to the massive volume of logged queries.

Recommended Rating: 3 stars (out of 5)

2. Using the PHPStorm Plugin

PHPStorm's plugin marketplace offers a plugin called MySQL Proxy, which, when configured to proxy the database in your code, displays all SQL logs. It is the only convenient tool our editor currently uses, as it doesn't require code changes, doesn't need MySQL database server permissions, and offers easy retrieval.

PHPStorm MySQL Proxy Plugin

Editor's Review:

This tool is similar to MyCAT as it can proxy and log SQL statements.

Drawback: While it supports searching, it lacks support for regular expression search and only supports text search.

Recommended Rating: 4 stars (out of 5)

These are several methods to view executed SQL queries in WordPress. Which method do you prefer, or do you have a better alternative? Please share your thoughts in the comments so our editor can provide more information accordingly.

Top comments (0)