Databases and drupal

Post Reply
KBleivik
Site Admin
Posts: 184
Joined: Tue Sep 29, 2009 6:25 pm
Location: Moss Norway
Contact:

Databases and drupal

Post by KBleivik »

1. Introduction

Since drupal is our preferred content management system, we will draw your attention to the Pro Drupal book more precisely Pro Drupal Development chapter 5 Working With databases. In a sense drupal has its own database language that is database agnostic building on the PHP PDO object: http://php.net/manual/en/book.pdo.php That means that you perform queries with the statement db_query(), instead of mysql_query() or pg_query(). The statements are prepared: http://www.php.net/manual/en/pdo.prepar ... ements.php to avoid sql injection attacks.

2. Bootstrap drupal so you can issue queries.

If you look in the bootstrap.inc file in the includes directory of your drupal installation, you find the following information:

Code: Select all

 * Ensures Drupal is bootstrapped to the specified phase.
 *
 * In order to bootstrap Drupal from another PHP script, you can use this code:
 * @code
 *   define('DRUPAL_ROOT', '/path/to/drupal');
 *   require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
 *   drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
 * @endcode
 *
 * @param $phase
 *   A constant telling which phase to bootstrap to. When you bootstrap to a
 *   particular phase, all earlier phases are run automatically. Possible
 *   values:
 *   - DRUPAL_BOOTSTRAP_CONFIGURATION: Initializes configuration.
 *   - DRUPAL_BOOTSTRAP_PAGE_CACHE: Tries to serve a cached page.
 *   - DRUPAL_BOOTSTRAP_DATABASE: Initializes the database layer.
 *   - DRUPAL_BOOTSTRAP_VARIABLES: Initializes the variable system.
 *   - DRUPAL_BOOTSTRAP_SESSION: Initializes session handling.
 *   - DRUPAL_BOOTSTRAP_PAGE_HEADER: Sets up the page header.
 *   - DRUPAL_BOOTSTRAP_LANGUAGE: Finds out the language of the page.
 *   - DRUPAL_BOOTSTRAP_FULL: Fully loads Drupal. Validates and fixes input
 *     data.
 * @param $new_phase
 *   A boolean, set to FALSE if calling drupal_bootstrap from inside a
 *   function called from drupal_bootstrap (recursion).
 *
 * @return
 *   The most recently completed phase.
 */
3. How to bootstrap drupal in the home directory of an add on domain.

Since drupal is installed in the home directory of my addon domain, I use the following code:

Code: Select all

<?php
define('DRUPAL_ROOT',  $_SERVER['DOCUMENT_ROOT']);
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
?>
4. Running queries against the database.

Now it is possible to run queries using db_query() as explained in chapter 5 of the book mentioned in the introduction.

5. Additional information and examples.

https://drupal.org/developing/api/database

http://api.drupal.org/api/drupal/core%2 ... database/8

Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests