Call to undefined function db_query()

Ajay on January 31st, 2008

Drupal provides the function db_query() to interact with database.

This function is defined in includes/database.inc file.

You have to include database.inc in your file to get db_query() working.

<?php include(“includes/database.inc”); ?>

However if you are trying to access drupal database from a non-drupal .php file then all you need to do is to include following lines at the top of your file.

require_once ‘./includes/bootstrap.inc’;
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

Add a Comment

pavani on November 9th, 2009

Hi,

I am new to drupal created a module for that I need to execute database queries in a .php file in that module folder.

I tried to implement the above code but I am getting a error that ./includes/cahce.inc not found error.

Can you help me out

Regards,
Pavani

[Reply]

bob Reply:

try to put the include code in a file situed in your root directory, because all files included in bootstrat.inc are included like that : ‘./includes/blablabla.inc’

[Reply]

Ajay on November 9th, 2009

Can you check if you have this file ./includes/cahce.inc ?
Also could you check the path for the above file?

[Reply]

pavani Reply:

@Ajay,

I have checked the path …
since I cannot change the path in bootstrap.inc

I changed the dir to the drupal installation directory from the modules folder and then included rather specifying the path at the require_once

this worked…

$drupal_path = “../”; // <—- change this one to fit yours
chdir($drupal_path);
chdir($drupal_path);
chdir($drupal_path);
chdir($drupal_path);
require_once "./includes/bootstrap.inc";

rather than
require_once "../../../../includes/bootstrap.inc";

thank you very much

Regards,
Pavani

[Reply]

ibrahim on March 4th, 2010

Hi:
I try to call function from drupal,s module file via standard ajax
but i get this error:
Call to undefined function db_query()
also i set include statement for (database.inc,database.sql.inc,..) and other data base file but it is not working
please any help

[Reply]