Direkt zum Inhalt

Devbits

Kleine Code-Schnipsel aus unserem Alltag mit Drupal, PHP und DevOps: Problem, Lösung, Code.

01.09.2026 | Dominik Wille

Test PHP database connection without Drupal

You need to debug the database connection.

<?php

include('web/sites/default/settings.php');

$dbHostName = $databases['default']['default']['host'];
$dataBaseName = $databases['default']['default']['database'];
$dbUserName = $databases['default']['default']['username'];
$dbPassword = $databases['default']['default']['password'];
$dbPort = $databases['default']['default']['port'];

$pdo = new PDO('mysql:host='.$dbHostName.';port='.$dbPort.';dbname='.$dataBaseName.'', $dbUserName, $dbPassword, array(
  // PDO::MYSQL_ATTR_SSL_CA    =>'ca.cert',
  PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => TRUE
  )
);

$query = $pdo->query('SHOW TABLES;');
print_r($query->fetchAll());

return $pdo;

Issue:

You need to debug the database connection.

A standalone script – for example a monitoring, backup, or migration helper – needs direct database access without bootstrapping the full Drupal framework. Duplicating database credentials in the script is undesirable since they would need to be kept in sync with settings.php.

Solution:

The snippet provided allows PHP debugging. Use it to check whether your database connection works on PHP level.

The script includes settings.php directly, which populates the $databases array as Drupal itself does during bootstrap. Credentials are then read from $databases['default']['default'], avoiding hardcoded or duplicated connection details.

A PDO connection is built from these values. PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => TRUE enforces SSL certificate verification on the connection – relevant when the database host requires or supports TLS, such as many managed hosting environments.

Since settings.php is plain PHP, including it outside of Drupal's bootstrap process only defines variables and does not require Drupal's autoloader or dependency injection container.

12.03.2026 | Pascal Crott, Michael Böker

Convert MySQL 8 Database Dump to MariaDB

You have a MySQL 8 Database Dump which needs to be converted into MariaDB for use with a Drupal setup.

cat olddump.sql | sed -e s/uca1400_as_ci/utf8mb4_general_ci/ | gzip > newdump.sql.gz

Issue

You have a MySQL 8 Database Dump which needs to be converted into MariaDB for use with a Drupal setup.

Solution

Use the command provided.

05.02.2026 | Michael Ebert

Make a database dump including data from an ignored table

You need a dump including the values of a datebase table that is ignored by your drush configuration.

drush sql:dump --structure-tables-list=cache,cachetags,'cache_*',history,'search_*',sessions,webprofiler

Issue:

You need a dump including the values of a datebase table that is ignored by your drush configuration.

Prerequisites:

You use drush in your setup and have a drush config file (e.g.: https://github.com/drush-ops/drush/blob/master/examples/example.drush.yml)  like this:

command:
  sql:
    dump:
      options:
        structure-tables-list:
          - cache
          - cachetags
          - 'cache_*'
          - history
          - 'search_*'
          - 'sessions'
          - 'watchdog'
          - 'webprofiler'

Solution:

This example overrides the settings for the ignored tables in your drush command to also get the data of the watchdog table.

18.12.2025 | Michael Böker

How to add a password field to an entity as the Field_UI has none to offer

You want to add a password field to an entity via the field_ui. It does not work, and – as a reminder to recurring devs – is not supposed to work. The UI is switched off in the core definition.

/**
 * Defines the 'password' entity field type.
 *
 * @FieldType(
 *   id = "password",
 *   label = @Translation("Password"),
 *   description = @Translation("An entity field containing a password value."),
 *   no_ui = TRUE,
 * )
 */

Issue:

You want to add a password field to an entity via the field_ui. It does not work, and – as a reminder to recurring devs – is not supposed to work. The UI is switched off in the core definition.

Solution:

If you need a password field, first evaluate the necessity to save a password in the database – this might create a serious security risk. 

If your project requires it anyway, you either have to create a custom widget for a textfield or use the module Password field.

13.11.2025 | Marc Hitscherich

List MySQL tables by size

You need to sort MySQL tables by size.

SELECT table_name AS `table`, round(((data_length + index_length) / 1024 / 1024), 2) `size in MB`
FROM information_schema.TABLES
WHERE table_schema = "DATABASE_NAME"
ORDER BY `size in MB` DESC
LIMIT 10;

Issue:

You need to sort MySQL tables by size.

Solution:

The snippet shows the size of the 10 biggest tables. DATABASE_NAME needs to be replaced by your database name.