By Paintbox on
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
Just bumping this thread
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.
mmm..
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..
I found this code easy
This is a simple snippet using drupal database abstraction layer from an external PHP file..
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..
Warning:
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 11I 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.
maybe
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.
Thanks for the help so far.
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:
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.
any.. but..
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.
..
Hope we could find the solution :D
Hehe okay I got it working
Hehe okay I got it working this time. Thanks for your help, I appreciated it.
Bingo
Wow. I doubt I could figure this all out by myself. Thanks everyone, I can confirm this works!
Where is?
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
../Thanks for your nice
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)
Drupal 6 update
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.
Also I was making a module
Also I was making a module for drupal 6 which requires a php script file. This was how I got around it:
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
error Cannot redeclare db_status_report
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
Finally!
It solved my problem. Thank you!
I know its too old but what
I know its too old but what about drupal 8 ?
In Drupal 7 i used this
--
Azhar uddin
Technical Lead
email : engr.azharuddin@gmail.com
skype : computer_jin