Hi everyone,

I am still pretty new to Drupal. How would I connect to the Drupal DB from an external PHP file?

I figure I need to have some includes, preferable the one with the DB connection data. But which one is that?
Also if I wanted to make us of the DB API, which files would I need to include? This last question isn't terribly important, the above is :)

Comments

Laurentvw’s picture

Just bumping this thread because I also want to know. I'll keep track of this post :) Let's hope someone can help us.

Edit: I found this: http://lists.drupal.org/pipermail/development/2005-March/001824.html but I haven't tried it (not much time right now). Let me know if you got it working with this method.

ilo’s picture

Seems like this link is about how to tell drupal to connect to other application database, instead of other applications connecting to drupal. I think this is not the way at all..

ilo’s picture

This is a simple snippet using drupal database abstraction layer from an external PHP file..

<?php
// include needed files
   include('includes/bootstrap.inc');
   include('includes/database.inc');
   include('includes/database.mysql.inc');

// Launch drupal start: configuration and database bootstrap
   conf_init();
   drupal_bootstrap(DRUPAL_BOOTSTRAP_CONFIGURATION);
   drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);

// Now you can use drupal database with drupal's dbal:
// Unlock user admin if blocked
   db_query("UPDATE {users} set status = 1 where uid = 1");

?>

Save as test.php and browse the file..

The more boostrap launched at the begining, the more drupal API avaiable for your php file.

Hope you find usefull..

Laurentvw’s picture

Warning: include(/includes/bootstrap.inc): failed to open stream: No such file or directory in C:\apache2triad\htdocs\livequery\news.php on line 6 Warning: include(): Failed opening '/includes/bootstrap.inc' for inclusion (include_path='.;C:\apache2triad\php;C:\apache2triad\php\smarty;C:\apache2triad\php\pear') in C:\apache2triad\htdocs\livequery\news.php on line 6 Warning: include(/includes/database.inc): failed to open stream: No such file or directory in C:\apache2triad\htdocs\livequery\news.php on line 7 Warning: include(): Failed opening '/includes/database.inc' for inclusion (include_path='.;C:\apache2triad\php;C:\apache2triad\php\smarty;C:\apache2triad\php\pear') in C:\apache2triad\htdocs\livequery\news.php on line 7 Warning: include(/includes/database.mysql.inc): failed to open stream: No such file or directory in C:\apache2triad\htdocs\livequery\news.php on line 8 Warning: include(): Failed opening '/includes/database.mysql.inc' for inclusion (include_path='.;C:\apache2triad\php;C:\apache2triad\php\smarty;C:\apache2triad\php\pear') in C:\apache2triad\htdocs\livequery\news.php on line 8 Fatal error: Call to undefined function: conf_init() in C:\apache2triad\htdocs\livequery\news.php on line 11
I used include('/includes/bootstrap.inc'); instead of include('includes/bootstrap.inc'); because i'm using a php file that resides in a sub directory. But it's still not working. Not sure what's wrong.

ilo’s picture

because include('/includes/bootstrap.inc'); /includes is not a relative path, but a complete path.. use includes/ , ../includes or ./includes or the correct path instead..

By the way.. this paths are for a file located in the drupal directory.. so point the path to that directory first and append the includes/files part of the path, otherwise it will not work.

If the error is not the "no such file or directory" for the included files, then you will have to clarify about the errors if you want more help.. a simple "it's not working" is not helpfull at all.

Laurentvw’s picture

Thanks for the help so far. Stupid me, I should have tried ../ before. It did work (kind of) because that error didn't show up anymore.
I'm getting another one instead though.
Here it is:

Warning: include_once(./sites/default/settings.php): failed to open stream: No such file or directory in C:\apache2triad\htdocs\includes\bootstrap.inc on line 243 Warning: include_once(): Failed opening './sites/default/settings.php' for inclusion (include_path='.;C:\apache2triad\php;C:\apache2triad\php\smarty;C:\apache2triad\php\pear') in C:\apache2triad\htdocs\includes\bootstrap.inc on line 243 Warning: include_once(./sites/default/settings.php): failed to open stream: No such file or directory in C:\apache2triad\htdocs\includes\bootstrap.inc on line 243 Warning: include_once(): Failed opening './sites/default/settings.php' for inclusion (include_path='.;C:\apache2triad\php;C:\apache2triad\php\smarty;C:\apache2triad\php\pear') in C:\apache2triad\htdocs\includes\bootstrap.inc on line 243 Warning: require_once(./includes/cache.inc): failed to open stream: No such file or directory in C:\apache2triad\htdocs\includes\bootstrap.inc on line 895 Fatal error: require_once(): Failed opening required './includes/cache.inc' (include_path='.;C:\apache2triad\php;C:\apache2triad\php\smarty;C:\apache2triad\php\pear') in C:\apache2triad\htdocs\includes\bootstrap.inc on line 895 

This means, I believe, that bootstrap.inc was successfully "accessed" but apparently bootstrap.inc also contains some includes on its own. Bootstrap is, for instance, trying to include ./sites/default/settings.php, which obviously doesn't work, since it's supposed to be ../ (in my case). Any solution for that? Thanks.

ilo’s picture

the solution...

http://es.php.net/manual/en/function.chdir.php
http://es.php.net/manual/en/function.getcwd.php

Read carefully because sometimes windows paths can lead to error.
..

<?php

// Go to drupal path..  
   $drupal_path = "../drupal-5.2/";  // <---- change this one to fit yours
   $cdir = getcwd();
   chdir($drupal_path);

// include needed files
   include('includes/bootstrap.inc');
   include('includes/database.inc');
   include('includes/database.mysql.inc');

// Launch drupal start: configuration and database bootstrap
   conf_init();
   drupal_bootstrap(DRUPAL_BOOTSTRAP_CONFIGURATION);
   drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);

// Once bootstrapped.. go bak to this directory..
   chdir($cdir);

// Now you can use drupal database with drupal's dbal:
// Unlock user admin if blocked
   db_query("UPDATE {users} set status = 1 where uid = 1");

?>

Hope we could find the solution :D

Laurentvw’s picture

Hehe okay I got it working this time. Thanks for your help, I appreciated it.

Paintbox’s picture

Wow. I doubt I could figure this all out by myself. Thanks everyone, I can confirm this works!

dsextonj’s picture

Where is the Drupal path?

../drupal-5.2/

Not sure which folder this is in.

Dale

Found it. It's the path Drupal is installed in. In my case just

../

rajeevk’s picture

Thanks for your nice documentation.

If my drupal is hosted then will it take that path if I want to work with it ?

Thanks,

Rajeev Kumar,
@drupler@bihar.social (ActivityPub)

suleyman’s picture

This almost works for me. I just had to remove the third include statement to make it work on Drupal 6. Replacing 'include' with 'require_once' did not do the trick.

I realize this is an old thread but just in case someone stumbles in here as I did.

nimrod98’s picture

Also I was making a module for drupal 6 which requires a php script file. This was how I got around it:

<?php

   $drupal_path = "../";  // <---- change this one to fit yours 
   $cdir = getcwd();
   echo $cdir;                 //just to check which directory you are at currently
   echo "<br>";
  chdir($drupal_path);
  chdir($drupal_path);
  chdir($drupal_path);
  chdir($drupal_path);
  $dire = getcwd();
   echo $dire;                 //just to check which directory you are at currently

My URL on my localhost pointed to: http://localhost/drupal6

As you probably know, drupal 6 puts the module in /drupal6/sites/all/modules/(name of module) so by using chdir a couple of times, it navigates the php to the /drupal6/ folder which was what I wanted.

EDIT: In case you don't know regarding drupal 6 (I certainly did not..) :

http://drupal.org/node/260834

In drupal 6 mysql_query=db_query and mysql_num_result=db_result and mysql_fetch_array=db_fetch_array

Ive’s picture

Great,
However I get this error, and failed to find out how to fix it.

Fatal error: Cannot redeclare db_status_report() (previously declared in C:\xampp\htdocs\...\includes\database.mysql.inc:20) in C:\xampp\htdocs\...\includes\database.mysqli.inc on line 39

masu0105’s picture

It solved my problem. Thank you!

computer_jin’s picture

I know its too old but what about drupal 8 ?

In Drupal 7 i used this

define('DRUPAL_ROOT', getcwd());
header('Access-Control-Allow-Origin: *');
include_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

--
Azhar uddin
Technical Lead
email : engr.azharuddin@gmail.com
skype : computer_jin