Majid Fouladpour
Majid Fouladpour

Reputation: 30252

Database access in Drupal

I am trying to customize a Drupal 6.x module. The original module does not make any calls to the database, but the customized version needs to pull some data from the database. The query should take place with an AJAX call. So I am trying to make a data provider to receive the call, query the database, and return the result (some json).

For the data provider I need a link to the database, but I don't want to create the link in the script, rather I want to find the script where db connection values are stored and include that file. But I don't seem to have any success in finding the file. Where is it located in a typical Drupal installation?

P.S. I don't want to make this into a Drupal module and follow all the conventions to create _hook(), ... functions. I want to make it straight forward and old-school.

Upvotes: 1

Views: 2198

Answers (4)

Eric M
Eric M

Reputation: 101

You can add your second database to the drupal settings file and switch back and forth when needed. Look here: How to connect to multiple databases within Drupal

Upvotes: 0

bkildow
bkildow

Reputation: 5153

An easy way if you are calling this file directly is to bootstrap drupal. For example you can do the following:

<?php
// Bootstrap Drupal
require 'includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
?>

This will allow you to access the full range of drupal API's including db_query(). For your use you may want to pass just DRUPAL_BOOTSTRAP_DATABASE instead of DRUPAL_BOOTSTRAP_FULL into drupal_boostrap(). See http://api.drupal.org/api/function/drupal_bootstrap/6 for reference.

Upvotes: 7

ceejayoz
ceejayoz

Reputation: 180004

Database settings should be in sites/default/settings.php.

Upvotes: 0

Kevin
Kevin

Reputation: 13226

"P.S. I don't want to make this into a Drupal module and follow all the conventions to create _hook(), ... functions. I want to make it straight forward and old-school."

Why? This is the -easiest- way to do it and you can still define the external db connection there and utilize the Drupal database layer, and you can do this from within the module you are customizing.

If you are not utilizing an external database (not Drupal) then you can just use the Database API.

http://api.drupal.org/api/group/database/6

Additional settings can be stored in settings.php but since you are customizing a module, I would have it right within the module so you don't have to hunt it down later.

Upvotes: 2

Related Questions