Hi,

I need to access the drupal database for a stand-alone PHP script which is in a sub folder in the root directory.

I added include_once('includes/bootstrap.inc') and drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE) in my script. I am able to query from the root (eg: http://www.mydomian/drupal/test.php). However I would prefer to put my script into a folder (eg: http://www.mydomian/drupal/myfolder/test.php), but I can't access the database.

I have tried to change the syntax, but I still can't access the database. I'm using a windows box. Please help.

Emmanuel

Comments

mooffie’s picture

However I would prefer to put my script into a folder

You can, but you'll have to do chdir('../../../../..') before you include Drupal's libraries. You may be able to do chdir() to return to your original directory, but if some Durpl function later does include('./something') it will fail.

That's sad. I've complained about this before, but it seems people aren't bothered.

--
Help save BLOCKQUOTE

erehm’s picture

I can't seem to find a simple "how to" anywhere. Like Emmanuel, I want to access Drupal from a stand-alone php script.

My test drupal installation is at /usr/share/drupal; http://www.foo.com/drupal gets me to my drupal home page.

I have added the following two lines to the top of my php script /usr/share/drupal/mypage.php:

include_once "includes/bootstrap.inc";
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

Then, logged in as root and cd to /usr/share/drupal, I type:

[eric@foo]$ php ./mypage.php

and I get:

PHP Notice:  Undefined index:  HTTP_HOST in /usr/share/drupal/includes/bootstrap.inc on line 204
PHP Notice:  Undefined index:  HTTP_HOST in /usr/share/drupal/includes/bootstrap.inc on line 261
PHP Notice:  Undefined index:  REMOTE_ADDR in /usr/share/drupal/includes/bootstrap.inc on line 889
PHP Notice:  Undefined index:  REMOTE_ADDR in /usr/share/drupal/includes/bootstrap.inc on line 830
PHP Notice:  Undefined index:  REQUEST_METHOD in /usr/share/drupal/includes/bootstrap.inc on line 466

Can someone just simply explain what I need to type or set to simply get past boostrap.inc and run my script?

(Aside: These Drupal forums are short on answers to simple "getting started" questions and my "Pro Drupal Development" book is no help either....)

erehm’s picture

Surprise, surprise. Despite the "PHP Notices" above, my script works. Still:

  1. Why does my script work if drupal/PHP is printing these messages?
  2. How do I get rid of these PHP Notice messages?
chadhester’s picture

Working on an older D6 site where I need to write some data to the Drupal database and use a few of the basic Drupal functions. This worked for me:

<?php
define('DRUPAL_ROOT', $_SERVER['DOCUMENT_ROOT']);
$cd = getcwd();
chdir(DRUPAL_ROOT); // Change scope to the Drupal path
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_CONFIGURATION); 
drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE); 
drupal_bootstrap(DRUPAL_BOOTSTRAP_VARIABLES);
chdir($cd); // Change back to the correct directory

?>

Thanks to the OP!

__________
Regards,
Chad Hester