Jump to Navigation

063 - Connecting to the Database in Drupal

Drupal automatically establishs a connection to the database as part of its normal bootstrap process,
so you do not need to worry about doing that.
If you are working outside Drupal itself (for example, you're writing a stand-alone PHP script or have existing
PHP code outside of Drupal that needs access to Drupal's database)
You would use the following approach.

// Make  Drupal PHP's current directory

chdir('full/path/to/your/drupal/installation');

// Bootstrap Drupal up through the database phase

include_once('./includes/bootstrap.inc');
drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);

// Now you can run queries using db_query().

$result = db_query('SELECT title FROM {node}');

 



Main menu 2

Story | by Dr. Radut