function db_like

7.x db_like($string)
8.x db_like($string)

Escapes characters that work as wildcard characters in a LIKE pattern.

The wildcard characters "%" and "_" as well as backslash are prefixed with a backslash. Use this to do a search for a verbatim string without any wildcard behavior.

For example, the following does a case-insensitive query for all rows whose name starts with $prefix:

$result = db_query(
  'SELECT * FROM person WHERE name LIKE :pattern',
  array(':pattern' => db_like($prefix) . '%')

Backslash is defined as escape character for LIKE patterns in DatabaseCondition::mapConditionOperator().


$string: The string to escape.

Return value

The escaped string.

Related topics

18 calls to db_like()
comment_form_validate in drupal/modules/comment/comment.module
Validate comment form submissions.
DatabaseBasicSyntaxTestCase::testLikeBackslash in drupal/modules/simpletest/tests/database_test.test
Test LIKE query containing a backslash.
DatabaseBasicSyntaxTestCase::testLikeEscape in drupal/modules/simpletest/tests/database_test.test
Test escaping of LIKE wildcards.
DrupalDatabaseCache::clear in drupal/includes/
Implements DrupalCacheInterface::clear().
EntityFieldQuery::addCondition in drupal/includes/
Adds a condition to an already built SelectQuery (internal function).

... See full list


drupal/includes/database/, line 2646
Core systems for the database layer.


function db_like($string) {
  return Database::getConnection()->escapeLike($string);