8.5.x database.inc db_and()
8.0.x database.inc db_and()
8.1.x database.inc db_and()
8.2.x database.inc db_and()
8.3.x database.inc db_and()
8.4.x database.inc db_and()
8.6.x database.inc db_and()
7.x database.inc db_and()

Returns a new DatabaseCondition, set to "AND" all conditions together.

Return value


Related topics

2 calls to db_and()
SearchQuery::parseSearchExpression in modules/search/search.extender.inc
Parses the search query into SQL conditions.
_node_query_node_access_alter in modules/node/node.module
Helper for node access functions.


includes/database/database.inc, line 2730
Core systems for the database layer.


function db_and() {
  return new DatabaseCondition('AND');


chrisrockwell’s picture

Simple db_and()

$query = db_select('Exhibitors', 'e');
$db_and = db_and();
$db_and->condition('ProductCategory', array(1,2,3), 'IN');
$db_and->condition('status', 'active');

Combine with a db_or() that uses a foreach()

$db_or = db_or();
foreach ($search_fields as $field) {
  $db_or->condition($field, "%$searchtext%", 'LIKE'); 
if (!is_null($search_category)) {
  $db_and = db_and();
  $db_and->condition('ProductCategory', array(1,2,3), 'IN');
tetmo’s picture

I have encountered a strange situation with db_and (as well as db_or) that requires a comment. I have two queries where the one that is executed depends on an if statement:

$query1 = db_select("my_table", 'kws');
$query1 -> join('node', 'n', 'n.nid = kws.nid');
$query1 -> fields('n', array('nid'));

$query1_and = db_and();
$query1_and -> condition('n.status', 1);
$query1_and -> condition('kws.kw', $something, 'IN');

$query1 -> condition($query1_and);

$or = ... // some or statements

$query2 = db_select("my_table", 'kws');
$query2 -> join('node', 'n', 'n.nid = kws.nid');
$query2 -> join('my_other_table', 'lse', 'n.nid = lse.sid');
$query2 -> fields('n', array('nid'));

$query2_and = $query1_and;
$query2_and -> condition($or); // line causing the problem

$query2 -> condition($query2_and);

$query = $query1;
$query = $query2;

$query -> execute();

The issue is with the line:$query2_and -> condition($or). It changes the value of $query1_and so that if I hit the line $query = $query1 then the $query1_and has now changed and the query gives the following error:

PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ...

The solution is to obviously recreate the code for $query2_and and remove $query2_and = $query1_and, but I am curious as to what is going on under the hood.

Island Usurper’s picture

By doing $query2_and = $query1_and;, you're not making a separate copy of those condition. $query2_and->condition($or); is going to change $query1_and like you see.

The solution is to clone the object: $query2_and = clone $query1_and;. This gives you truly independant objects that just happen to look the same. I've used this trick on the actual query objects when I need to do a few similar queries at once.