There's a database table. I know only one field name on the database table: username. How do I view remaining field names on the table?

namespace Drupal\fixx_helper\Controller;

use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;

class AutocompleteController extends ControllerBase
   public function handleAutocomplete(Request $request)
      $results = [];

      $input = $request->query->get('q');
      $database = \Drupal::database();
      $my_query = $database->query("select distinct username from banner_exports.ws_students where username like '$input%'");

      while($result = $my_query->fetchAssoc()){
         $matches[] = array('value' => $result['username'], 'label' => $result['username']);
       return new JsonResponse($matches);



There are multiple table locations for Drupal field data: the base User entity table users_field_data & the user__$FIELD tables that contain the custom fields defined by site builder / developer. If you need field data from a DB query, then you're going to need to build a query with a few joins to put it all together.

If you're only looking for the field names then it's much earlier to use the entity field manager to pull the base & custom fields:

\Drupal::service('entity_field.manager')->getFieldDefinitions('user', 'user')
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.