PHPMyAdmin: Useful Strategies for Managing Drupal

I don't always crack open my Drupal database... but, when I do: it always leaves me a happier person. If you're like me, perhaps you often overlook the power of your Drupal site's database. It's effectively a warehouse, and Drupal is a warehouse manager: retrieving all the items your site-visitors request and updating what sits on your warehouse shelves--all 'on the fly'. Still, there are limits to Drupal's ability. It may be good at coordinating between end-users and the proverbial warehouse, but it's not really an inventory manager. To get a really good feel for what's going on in you Drupal database, you'll need a different tool: PHPMyAdmin. Here are some simple ways I like to leverage this tool in my Drupal development workflow.

Clearing Drupal's Site Cache

I've covered this before in a post on how database cache can kill your Drupal site. It's worth mentioning again: if you don't have Drush and you're without access to Drupal, you can clear your site's cache in PHPMyAdmin by simply dropping any of the tables appended with 'cache_'. From the "Structure" tab of your database, filter for 'cache_' and drop everything that comes up:

clearing a Drupal site's cache from PHP my admin

Viewing User Access History

Yes--you can actually use PHPMyAdmin to, in a sense, audit which nodes your Drupal site users have been accessing. You can do this in the 'history' table, although there are a couple of provisos: you'll need to know the users uid and you'll need to have a firm grasp of node ID's (nid). The uid for a given user is quite easy to ascertain in PHPMyAdmin: just head to the users_field_data table for an overview of uid's and their corresponding human-readable names (as well as e-mail addresses). The "history" found in the history table is all found in node ID format (nid); if you're at all unclear on what page an nid corresponds to, just enter it into your site's address: since at least Drupal 8 (I believe), the format should be: www.mysite.com/node/nid

the history table in a drupal database, as viewed with php my admin
There it is!--throw privacy to the wayside and gaze in awe at what pages users have been viewing. I can't give any further screenshots beyond this one for security reasons...

It's not as informative, perhaps, as looking as someone's browser history; in fact, the table doesn't look to go back all that far (about a month, give or take, on the databases I've bothered to look at). Still, if you wanted to determine whether a user had recently accessed a certain page, you could theoretically do it. There are some obvious "privacy" issues that may arise from doing this (I have literally know idea what kind of site you run or what kind of data you and your users access). If you're dealing with HIPAA and sensitive, data, you'd want to be very careful with how you use this and who has access to your Drupal database. You'd also want to be sure by viewing this information you're not violating any of your own license agreements--use at your own risk!

Querying the Node Body Across All Site Pages:

This can actually be done by Drupal with the power of Drupal views. All the same, you might not always have access to Drupal. Either way, though, I'd wager that PHPMyAdmin is the more powerful of the two tools when it comes to doing this. Where PHPMyAdmin shines above Drupal is in it's ability to not just find expressions in the node body, but also in it's ability to replace them. NOTE: This is a particularly dangerous tool (you need to be very precise with a search and replace) but it can save you literally hours of work on a larger site.

Let's look first at how you can query the node body--an easy enough start: find the node__body table and open it up. You'll see an entity_id column (essentially the same as nid) identifying each node on your site; you'll also see a body_value column containing the raw value for each page body (including HTML):

the node__body table of a drupal site's database
Pay special attention to the "search" tab in this screenshot!

You might use this table to track down a specific reference on your site: i.e., pages containing a specific company name, or HTML elements having a specific class. In order to search the table, you can simply head to the "search" tab: in the screenshot below I'll run a regular expression search for the word "analytics" on a range of node id's between 1 and 20 (leave the "entity_id" field blank if you want to search against all pages):

running a database table search with php my admin
Magic! See that "Find and replace" button right under the "Search" tab--take note!

Let's see what results that turns up:

results of a php my admin search on the node__body table
Cool!

So, what if I needed to replace any and all references to analytics? We'll, I'd obviously have to consider two different references: "Analytics" (a Google product and service) and "analytics" (as in, the data obtained from a service like Google's, or Plausible.io's). In either case, though, I can use the "Find and replace" button that appears on the "Search" tab (see second screen grab above). Let's find any references to "Analytics" (with a capital 'A') and replace it with "Google's crimes against humanity":

running a search and replace with php my admin
Changing perceptions of the internet one find and replace at a time...

PHPMyAdmin will give you a sanity check before actually making the replace. The output is a bit too big to neatly display in this article, but suffice it to say you can proof the change before it's made. Even with the sanity check, you won't want to run a find and replace without first having a backup of your database!!!

Querying Node Field Data Across All Site Pages: i.e., Node Class

That's right, just like querying the node body, you can query node field data--like node class settings. Node class is a great tool for adding css classes to a node at the HTML <article> level. I use it all the time, but it's easy for me to lose track of which nodes have what class assigned to them. What if, for example, I wanted to find every node with a specific class. I can also do that with PHPMyAdmin. I head to the node_field_data table and click on the "search" tab once again; on the search page, I can perform a search on the node_class field. I have a global class that I often assign to nodes called "sales-page" let's see if we can get a sense of which nodes use it:

using php my admin to search for a node class in a drupal database
ready... set... GO!

Sure enough, there are quite a few pages using that class:

node class query results with php my admin
Look at that, I even get an nid for each result!

This is extremely useful: if I wanted to change that global class, I'd have a sense of each and every page it impacts! Super cool! With the power of search and replace, I could even change the name of the class in my style sheet and then update every page it touches in one fell swoop!

Final Thoughts

That's all I've got for the time being. PHPMyAdmin is a resource I often overlook, even within my own workflow. While I was compiling this post, I actually got really excited about how powerful PHPMyAdmin actually is. It left me anxious to explore the platform further. As with all things, so long as you have a backup, you really shouldn't be afraid to get your hands dirty. Enjoy!