db_type_placeholder

Definition

db_type_placeholder($type)
includes/database.inc, line 552

Description

Given a Schema API field type, return the correct %-placeholder.

Embed the placeholder in a query to be passed to db_query and and pass as an argument to db_query a value of the specified type.

Parameters

$type The Schema API type of a field.

Return value

The placeholder string to embed in a query for that type.

Related topics

Namesort iconDescription
Schema APIA Drupal schema definition is an array structure representing one or more tables and their related keys and indexes. A schema is defined by hook_schema(), which usually lives in a modulename.install file.

Code

<?php
function db_type_placeholder($type) {
  switch ($type) {
    case 'varchar':
    case 'char':
    case 'text':
    case 'datetime':
      return "'%s'";

    case 'numeric':
      // Numeric values are arbitrary precision numbers.  Syntacically, numerics
      // should be specified directly in SQL. However, without single quotes
      // the %s placeholder does not protect against non-numeric characters such
      // as spaces which would expose us to SQL injection.
      return '%n';

    case 'serial':
    case 'int':
      return '%d';

    case 'float':
      return '%f';

    case 'blob':
      return '%b';
  }

  // There is no safe value to return here, so return something that
  // will cause the query to fail.
  return 'unsupported type '. $type .'for db_type_placeholder';
}
?>
 
 

Drupal is a registered trademark of Dries Buytaert.