Same name in this branch
  1. 6.x includes/database.pgsql.inc \db_query_temporary()
  2. 6.x includes/database.mysqli.inc \db_query_temporary()
  3. 6.x includes/database.mysql.inc \db_query_temporary()
Same name and namespace in other branches
  1. 4.7.x includes/database.pgsql.inc \db_query_temporary()
  2. 4.7.x includes/database.mysqli.inc \db_query_temporary()
  3. 4.7.x includes/database.mysql.inc \db_query_temporary()
  4. 5.x includes/database.pgsql.inc \db_query_temporary()
  5. 5.x includes/database.mysqli.inc \db_query_temporary()
  6. 5.x includes/database.mysql.inc \db_query_temporary()
  7. 7.x includes/database/database.inc \db_query_temporary()
  8. 8.9.x core/includes/database.inc \db_query_temporary()

Runs a SELECT query and stores its results in a temporary table.

Use this as a substitute for db_query() when the results need to be stored in a temporary table.

User-supplied arguments to the query should be passed in as separate parameters so that they can be properly escaped to avoid SQL injection attacks.

Note that if you need to know how many results were returned, you should do a SELECT COUNT(*) on the temporary table afterwards. db_affected_rows() does not give consistent result across different database types in this case.

Parameters

$query: A string containing a normal SELECT SQL query.

...: A variable number of arguments which are substituted into the query using printf() syntax. The query arguments can be enclosed in one array instead. Valid %-modifiers are: %s, %d, %f, %b (binary data, do not enclose in '') and %%.

NOTE: using this syntax will cast NULL and FALSE values to decimal 0, and TRUE values to decimal 1.

$table: The name of the temporary table to select into. This name will not be prefixed as there is no risk of collision.

Return value

A database query result resource, or FALSE if the query was not executed correctly.

Related topics

1 string reference to 'db_query_temporary'
drupal_error_handler in includes/common.inc
Log errors as defined by administrator.

File

includes/database.pgsql.inc, line 319
Database interface code for PostgreSQL database servers.

Code

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