db_prefix_tables

includes/database.inc, line 58

Versions
4.6 – 6
db_prefix_tables($sql)

Append a database prefix to all tables in a query.

Queries sent to Drupal should wrap all table names in curly brackets. This function searches for this syntax and adds Drupal's table prefix to all tables, allowing Drupal to coexist with other systems in the same database if necessary.

Parameters

$sql A string containing a partial or entire SQL query.

Return value

The properly-prefixed string.

Related topics

▾ 4 functions call db_prefix_tables()

db_next_id in includes/database.pgsql.inc
Return a new unique ID in the given sequence.
db_query in includes/database.inc
Runs a basic query in the active database.
db_queryd in includes/database.inc
Debugging version of db_query().
db_query_range in includes/database.pgsql.inc
Runs a limited-range query in the active database.

Code

<?php
function db_prefix_tables($sql) {
  global $db_prefix;

  if (is_array($db_prefix)) {
    if (array_key_exists('default', $db_prefix)) {
      $tmp = $db_prefix;
      unset($tmp['default']);
      foreach ($tmp as $key => $val) {
        $sql = strtr($sql, array('{'. $key. '}' => $val. $key));
      }
      return strtr($sql, array('{' => $db_prefix['default'], '}' => ''));
    }
    else {
      foreach ($db_prefix as $key => $val) {
        $sql = strtr($sql, array('{'. $key. '}' => $val. $key));
      }
      return strtr($sql, array('{' => '', '}' => ''));
    }
  }
  else {
    return strtr($sql, array('{' => $db_prefix, '}' => ''));
  }
}
?>
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.