How can I run a database query in code? I want something like SELECT nid FROM node WHERE type = "blablabla".

How do I do this in Drupal 8 PHP code?
You would use the database abstraction layer.

$query = \Drupal::database()-&gt;select('node', 'n');
$query-&gt;addField('n', 'nid');
$query-&gt;condition('n.type', 'blablabla');
$results = $query-&gt;execute();


As for executing the query, you can also fetch results in various ways.

Examples:

$query = \Drupal::database()-&gt;select('node', 'n');
$query-&gt;addField('n', 'nid');
$query-&gt;condition('n.type', 'blablabla');
$results = $query-&gt;execute()-&gt;fetchAll();


$query = \Drupal::database()-&gt;select('node', 'n');
$query-&gt;addField('n', 'nid');
$query-&gt;condition('n.type', 'blablabla');
$results = $query-&gt;execute()-&gt;fetchAllAssoc('nid');


If you are within a class, you should inject the appropriate classes in your constructor instead of use the static Drupal container. But since you are in a preprocess function, \Drupal::database() is fine.

From core/lib/Drupal.php:

 /**
   * Returns the current primary database.
   *
   * @return \Drupal\Core\Database\Connection
   *   The current active database's master connection.
   */
  public static function database() {
    return static::getContainer()-&gt;get('database');
  }


Depending on your use case(s), you can also leverage EntityQuery:

$query = \Drupal::entityQuery('node');
$query-&gt;condition('type', 'blablabla');
$results = $query-&gt;execute();


Please note that you may want to use node_field_data table instead of node, but your query is pretty generic so it is hard to tell without a better query.

Example, get all nodes of type that are published:

$query = \Drupal::database()-&gt;select('node_field_data', 'nfd');
$query-&gt;addField('nfd', 'nid');
$query-&gt;condition('nfd.type', 'blablabla');
$query-&gt;condition('nfd.status', 1);
$results = $query-&gt;execute();


Or:

$query = \Drupal::entityQuery('node');
$query-&gt;condition('type', 'blablabla');
$query-&gt;condition('status', 1);
$results = $query-&gt;execute();