How to create a select query in Drupal 10

To create a select query in Drupal 10

use Drupal\Core\Database\Database; 

$conn = Database::getConnection();

 // To fetch a list of records in an array of object format
 $query = $conn->select('users_field_data','ufd')
   ->fields('ufd',['uid','name','mail'])
   ->execute()
   ->fetchAll();

 // To fetch an associative array of record
 $query = $conn->select('users_field_data','ufd')
   ->fields('ufd',['uid','name','mail'])
   ->execute()
   ->fetchAssoc();

 // To fetch a single field based on a condition
 $query = $conn->select('users_field_data','ufd')
   ->fields('ufd',['uid'])
   ->condition('uid', 1, '=')
   ->execute()
   ->fetchField();


 

Category