~/drupal/drupal-db-query-fetch-active-user-accessed-within-last-one-year.md

Drupal DB Query Code to Fetch Active Users and Accessed Website Within last One Year

1 min readDecember 30, 2021by Gorav Singal#drupal#mysql
// TL;DR

Use Drupal entityQuery with a condition on the access timestamp to find active users who visited within the last year, then export results to JSON.

Drupal DB Query Code to Fetch Active Users and Accessed Website Within last One Year

Introduction

Here, we will see the drupal code to fetch all the active users (not blocked) and who accessed the website within last year.

We will also convert the output to JSON and save the JSON in a file.

This can be useful if you want to fetch users who are inactive. You may want to send them an email to login, or you may want to disable their accounts due to inactivity.

Drupal DB Code


//1 year seconds
$seconds = 3600 * 24 * 365;
$newtime = time() - $seconds;

$baseFolder = 'your_folder/';

$query = \Drupal::entityQuery('user')
    ->condition('status', '1')
    ->condition('access', '0', '!=')
    ->condition('access', $newtime, '<=');
$uids = $query->execute();

foreach ($uids as $uid) {
  $filename = $baseFolder.$uid.'.json';

  $user = user_load($uid);
  $data = \Drupal::service('serializer')->serialize($user, 'json', ['plugin_id' => 'entity']);
  
  print('<br/>Writing to file: '.$filename);
  file_put_contents($filename, $data); 
}

If your users are a lot, you can use pagination.


//1 year seconds
$seconds = 3600 * 24 * 365;
$newtime = time() - $seconds;

$baseFolder = 'your_folder/';

$query = \Drupal::entityQuery('user')
    ->condition('status', '1')
    ->condition('access', '0', '!=')
    ->condition('access', $newtime, '<=')
    ->range(0, 100);
$uids = $query->execute();

foreach ($uids as $uid) {
  $filename = $baseFolder.$uid.'.json';

  $user = user_load($uid);
  $data = \Drupal::service('serializer')->serialize($user, 'json', ['plugin_id' => 'entity']);
  
  print('<br/>Writing to file: '.$filename);
  file_put_contents($filename, $data); 
}

Mysql Query to fetch All users who accessed website within last 1 year

select uid, name, status, from_unixtime(created), from_unixtime(changed), 
from_unixtime(access), from_unixtime(login) 
from users_field_data 
where status=1 and access!=0 
and access >= DATE_SUB(NOW(), INTERVAL 1 YEAR) 
limit 1000, 10;

Earlier, I wrote about the Drupal Code to Fetch Active Users

$ subscribe --weekly

One deep dive a week, in your inbox

System design, AI engineering, trading systems and interview prep. Tested code, real numbers. Free, and you can unsubscribe any time.

or read it on gyanbyte.substack.com

Share

Related Posts

Drupal Mysql Query to Fetch User Field Details and its Alias

Drupal Mysql Query to Fetch User Field Details and its Alias

Introduction In this posr, we will see how to prepare mysql query to fetch user…

Fix Drupal MySQL Error: Got a Packet Bigger Than max_allowed_packet

Fix Drupal MySQL Error: Got a Packet Bigger Than max_allowed_packet

Introduction While this topic may applicable to all mysql/mariadb users who…

Drupal Code to run Database Query to Fetch Active User Details, Count, Pagination

Drupal Code to run Database Query to Fetch Active User Details, Count, Pagination

Introduction This post shows code to run database query to fetch active user…

Drupal 8 - How to Theme Form and its Fields with reordering fields

Drupal 8 - How to Theme Form and its Fields with reordering fields

Introduction In this post, we will see how to theme form and its fields…

Drupal 8 - How to create a Page with admin access and create its menu entry in Reports (No Coding)

Drupal 8 - How to create a Page with admin access and create its menu entry in Reports (No Coding)

Introduction I needed a report page, where I wanted to have some information…

Drupal 7 - Code for Exporting all your content nodes in json files

Drupal 7 - Code for Exporting all your content nodes in json files

Introduction When I migrated all of my drupal-7 website to drupal-8, I wrote…

Latest Posts

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

In the previous article I argued that AI is not killing software engineering…

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

A few months back I approved a PR that was almost entirely AI-generated. It…

How to Debug AI-Generated Code: Understand First, Fix Last

How to Debug AI-Generated Code: Understand First, Fix Last

In the last article I wrote about reviewing AI code before it goes in. This one…

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

Every few weeks somebody sends me a scary headline — “AI will replace all…

The Transactional Outbox Pattern: Never Lose an Event Again

The Transactional Outbox Pattern: Never Lose an Event Again

This is one of those patterns that looks like nothing until you get burnt once…

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Here is a question I like to ask in interviews: your bank account shows a…