I have written a PHP script for the programmatic creation of nodes:


define('DRUPAL_ROOT', getcwd());
$_SERVER['REMOTE_ADDR'] = "localhost";
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';

// Get a list of Office ID's
$office_ids = db_query('SELECT OFFICE_ID FROM sage.OFFICE');

// Loop through and edit each user
foreach($office_ids as $office) {

  // Create a new node object
  $node = new stdClass();

  // Set our new node content type
  $node->type = "office";

  // Set the node title
  $node->title = "Office " . $office->office_id;

  // Set the language
  $node->language = LANGUAGE_NONE;

  // Set the UID of the author of the node; or use $node->name
  $node->uid = 1;

  // Set node details
  $node->body[$node->language][0]['value'] = $bodytext;
  $node->body[$node->language][0]['summary'] = text_summary($bodytext);
  $node->body[$node->language][0]['format'] = 'filtered_html';

  $path = 'office-' . $office->office_id;
  $node->path = array('alias' => $path);

  if($node = node_submit($node)) {
    echo "Node with nid" . $node->nid . " saved!\n";

When I run the above script, I can see my NIDs incrementing (and I assume this means my nodes are created), however when I go and check in the 'nodes' table in the database, I see nothing added.

Does anything know what could be wrong?


The $node object you pass to node_save() wouldn't get it's nid member populated unless the node has been successfully saved to the node table in the database.

The auto-increment ID actually comes from the database table so if the $node object has a nid, you can pretty much guarantee the record has been successfully inserted into the table.

Make sure that the software you're using to view the table doesn't have some sort of paging turned on, meaning that only the first x number of records are actually showing in your results. Most MySQL UI software (PHPMyAdmin, Navicat, etc.) limit the number of results you see in a table by default; if you want to see more you need to change the setting.

If you want to do a quick check run the following query on your database:


Note down that number, then run your code again. Finally run the above query one more time. If the nid you get the second time around is greater than the one from the first time round, you know the record has definitely been added to the database.

  • Clive, I just did that but my nid has not changed, therefore not causing any nodes to be added to my 'node' table. Would you have any ideas as to why my code is not adding? – chlong Apr 3 '12 at 17:12
  • 1
    I can't think of a single good reason why it's not working, if you step through the node_save() function you'll see that it throws an error if it fails to save to the node table. Since you don't get that error it would suggest the record is definitely being saved. Silly question I'm sure: are you absolutely certain you're connecting Drupal to the same database as you're viewing once you've run your code? – Clive Apr 3 '12 at 17:16
  • 1
    You'll need to add another database connection in settings.php, or build up the connection as an array on the fly, to make that work. There's some pretty comprehensive instructions here that should help you out – Clive Apr 3 '12 at 18:03
  • 1
    All in favour of @Clive being renames as Captain Helpful? ;-) – Chapabu Apr 3 '12 at 21:15
  • 1
    @Chapabu Heh, more like Captain There's-nothing-good-on-the-telly ;) – Clive Apr 4 '12 at 0:40

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.