The following code is flawed:

<?php
function db_query_temporary($query) {
  $args = func_get_args();
  $tablename = array_pop($args);
  array_shift($args);

  $query = preg_replace('/^SELECT/i', 'CREATE TEMPORARY TABLE '. $tablename .' AS SELECT', db_prefix_tables($query));
  if (isset($args[0]) and is_array($args[0])) { // 'All arguments in one array' syntax
    $args = $args[0];
  }
  _db_query_callback($args, TRUE);
  $query = preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback', $query);
  return _db_query($query);
}
?>

Notice the line:

$query = preg_replace('/^SELECT/i', 'CREATE TEMPORARY TABLE '. $tablename .' AS SELECT', db_prefix_tables($query));

In this line, the query gets processed by db_prefix_tables, but tablename does not.
The end result is that when "{mytablename}" gets evaluated in both occurances, the temporary table will be called "{mytablename}" but the query might call it "drupaldb_mytablename"

So when query tries to access the temporary table, it cannot because it is being called by the wrong name.

The solution is easy, wrap the tablename call with the db_prefix_tables() function.

I have attached a patch that fixes this for all occurrences that I could find.

Comments

miglius’s picture

subscribe

thekevinday’s picture

Version: 6.9 » 6.x-dev
Issue tags: +database

This is still a problem, updating affected version.

Status: Active » Closed (outdated)

Automatically closed because Drupal 6 is no longer supported. If the issue verifiably applies to later versions, please reopen with details and update the version.