Call to undefined function db_query() 5

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);

5 thoughts on “Call to undefined function db_query()

  1. Reply pavani Nov 9,2009 12:23 pm

    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

  2. Reply Ajay Nov 9,2009 1:41 pm

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

  3. Reply pavani Nov 10,2009 11:05 pm

    @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

  4. Reply ibrahim Mar 4,2010 5:07 pm

    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

  5. Reply bob Aug 17,2011 3:04 pm

    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’

Leave a Reply to Ajay Cancel Reply