0

I am doing a simple insert query using the db_insert call; no matter where I try this, I am getting the same result. Is this an error with the Drupal 7 DAL, or am I doing something wrong?

The script is not running multiple times, nor Does the query run multiple times; I am using the following in a custom PHP file to generate the data.

chdir('../');

define('DRUPAL_ROOT', getcwd());

require_once DRUPAL_ROOT . '/includes/bootstrap.inc';

drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

$query = db_insert("table_name")
         ->fields(array('nid' => 10, 'width' => 200, 'height' => 800))
         ->execute();

I want to insert one record with the following data.

  • nid: 10
  • width: 200
  • height: 800

Running this always results in 2 exact records in the database, without errors. Could it be anything to do with not currently having the table I am inserting into inside my modules .install file? Maybe Drupal is doing something daft because it can't find the database schema info?

bumped to the homepage by Community yesterday

This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.

  • 2
    You know that there is db_merge just to prevent duplicate records? ;) – Mołot Feb 12 '14 at 10:45
0

Not running twice, are you sure? (Cue in Gandalf asking Radagast.) This reeks of poormanscron (which is in D7 core) calling back the page. You can disable that (if you have external cron properly set up) at admin/config/system/cron by setting "Run cron every" to Never.

  • yes I am sure :) - I had a custom PHP script which bootstrapped Drupal Full (DRUPAL_BOOTSTRAP_FULL) and I went to this script direct in terminal or browser, ran it and there would be 2 entries every time I tried it- changing my bootstrap to just Database (DRUPAL_BOOTSTRAP_DATABASE) fixed this - I'm not too sure why at the moment but the script is only 10 lines including the bootstrap phase, so I can only guess it's something recursive in the bootstrap phase that was causing it. – codeguy May 4 '13 at 21:20
  • If you went to this script in the browser then what I described could've happened. – user49 May 5 '13 at 0:41
  • I checked and cron was already set to never as the proper cron job is setup already - thank you though! – codeguy May 7 '13 at 15:21
0

This may happen if your db_insert call is executed twice - for instance because the function you've put it inside is called twice by Drupal when it is processing whatever you've asked it to process.

You can use the watchdog function to track function calls (and other events).

But to answer the exact questions you're asking:

could it be anything to do with not currently having the table I am inserting into inside my modules .install file?

No, Drupal will not even look inside the .install file unless you install or un-install the module.

maybe Drupal is doing something daft because it can't find the DB schema info?

Nope.

is this an error with the Drupal 7 DAL?

This is unlikely. The DAL is used daily by thousands of programmers, and this is not a known problem.

or am I doing something wrong?

I would not rule that out.

  • thank you for your answer but definitely not running twice - first thing I checked – codeguy May 3 '13 at 14:33
  • sorry about that, I should have posted some more information about the problem- have done so now, thank you for your time it was appreciated! :) – codeguy May 3 '13 at 15:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.