Run Query in phpMyAdmin

  1. Open phpMyAdmin
  2. Select the database you’d like to run a query on
    • You must select a database from the left column first
  3. Select SQL
  4. Look for the database name again above the text field, confirm this is the correct database you intend to run a query on
  5. Write or paste your query in the text field
  6. Click GO
    • Some queries will require a second confirmation in order to run.

NOTE: If you are running UPDATE or INSERT queries, these will change or add data to your database. You should make a backup before doing this to be safe.


Custom Database Prefix

Queries must be run in SQL and will most likely reference specific table names. Tables will not always be named the same thing from site to site, however.

Database prefixes can be changed for security reasons and may have been done so by a previous host if you migrated the site. Changing your database prefix means all of your tables are titled something new and are not using default titles. A custom prefix can be any string of random characters.

If you are running a query you must ensure you are taking into account if a custom database prefix is being used in order to properly run a query.

The default WordPress database prefix is wp_ and default tables are titled like this:

In this example, there is a custom prefix of wp_zgs0q4pna9_ and the table names look like this:

In this example, there is a custom prefix of test_ and the table names look like this:


Find the Database Prefix

If you cannot clearly distinguish your table names and prefix just from looking at your tables, you will need extra steps to locate which prefix (and subsequently, which tables) you are active.

  1. Connect to your site’s filesystem using SFTP
  2. Download the file wp-config.php from the root directory
  3. Open this file and locate the line $table_prefix
  4. Your database prefix will be within the quotes
    • In this example the prefix is wp_zgs0q4pna9_

If you need to change your database prefix, check out our guide.

NOTE: It is common but not required that your database prefix include a trailing underscore at the end, however it is not required.


Run SQL Query with a Custom Database Prefix

You’ve found a SQL query you want to run but you’ve discovered you have a custom database prefix so the command doesn’t work correctly. You’ll need to slightly modify the SQL query to work on your site.

Wherever you see wp_ in a SQL query, just replace it with your prefix. This may be more than one location if the query interacts with multiple tables.

Example

For this example, we use the prefix: wp_zgs0q4pna9_

Default query example:

SELECT SUM(LENGTH(option_value)) FROM wp_options WHERE autoload = 'yes';

This command targets the table wp_options but we need it to target the wp_zgs0q4pna9_options table. Replace wp_ with your custom prefix for the command appropriate for this site.

Customized query:

SELECT SUM(LENGTH(option_value)) FROM wp_zgs0q4pna9_options WHERE autoload = 'yes';

NOTE: We’ve used an example database prefix. Yours will be different and will need to be modified appropriately.